regexprep delete parenthesis in a sentence. not working

1 visualización (últimos 30 días)
Kristin Bogar
Kristin Bogar el 1 de Dic. de 2021
Editada: dpb el 2 de Dic. de 2021
im trying to remove the bolded area. because of the () regexprep doesnt seem to find the sentance to remove. is there a work around for this so i can get rid of it all including the ()?
text file
{
Name = PTC_MATERIAL_SUB_TYPE
Type = String
Default = 'LINEAR'
Access = Locked
},
{
Name = PTC_THERMAL_CONDUCTIVITY
Type = Real
Default = NaN W/(m K)
Access = Full
},
{
Name = PTC_MASS_DENSITY
Type = Real
Default = 1.000000 lbm/in^3
Access = Full
fid = fopen('trial.txt', 'rt') % Open source file.
g= fscanf(fid,'%c')
expression7 = '\n{\n Name = PTC_THERMAL_CONDUCTIVITY\n Type = Real\n Default = NaN W/(m K)\n Access = Full\n},\n{';
replace7 = '';
newStr7 = regexprep(g,expression7,replace7);

Respuestas (1)

dpb
dpb el 2 de Dic. de 2021
Editada: dpb el 2 de Dic. de 2021
It's short enough, I'll repost the working code from previous Answer again...
txt=textread('bogar.txt','%s','whitespace','','delimiter',newline); % read file a cellstr array
idxHdr=find(contains(txt,'{')); % find the section headers
idxTrlr=find(contains(txt,'},')); % and the closing trailers
idxNaN=find(contains(txt,'NaN')); % then where the offending NaNs are
ixDel=flip(interp1(idxHdr,1:numel(idxHdr),idxNaN,'previous')); % find sections containing NaN
for i=ixDel(:).' % iterate over sections
txt(idxHdr(i):idxTrlr(i))=[]; % remove between header/trailer
end
NB: This version includes the refinement in the Comment earlier to search for the opening "{" instead of "Name" since the sections are not nested.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by