regexprep delete parenthesis in a sentence. not working
1 view (last 30 days)
Show older comments
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);
1 Comment
Answers (1)
dpb
on 2 Dec 2021
Edited: dpb
on 2 Dec 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.
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!