Ignoring comments in lines when parsing
11 views (last 30 days)
Show older comments
I am parsing a file with the following code:
C = textscan(fid,fmt,opt{:},'CommentStyle','#');
This works in ignoring comments that begin a line such as this:
apple = 12
#mycomment
but how do I ignore comments that are in a line.
For example, I want to be able to parse the following line:
apple = 12 #mycomment
Currently, the comment will not be ignored. Within my current scheme, how do I ignore this? Thanks!
0 Comments
Answers (2)
Adam Danz
on 9 May 2019
Edited: Adam Danz
on 9 May 2019
I think you'll have to clean up the text after you've imported it.
Use regexp() to find the location of '#' in each element of C. Then trim everything from that index onward, for each element of C.
% Example text
C = {'pi = 3.14159'; 'apple = 12 #my comment'; 'banana = 13 #one #two comments'; 'orange = 6';
'grapes = 8 #comment'};
% Find first location of '#' in each element
indices = regexp(C, '#', 'Once');
% remove those sections (and any leading/trailing white space)
trimmedTxt = cellfun(@(x,y)strtrim(x(1:y-1)), C, indices, 'UniformOutput', false);
% Add in the elements of C that didn't have any '#' characters
trimmedTxt(cellfun(@isempty,trimmedTxt)) = C(cellfun(@isempty,trimmedTxt));
Result
trimmedTxt =
5×1 cell array
{'pi = 3.14159'}
{'apple = 12' }
{'banana = 13' }
{'orange = 6' }
{'grapes = 8' }
0 Comments
Walter Roberson
on 9 May 2019
S = fileread(TheFileName);
S = regexprep(S, '#.*$', '', 'dotexceptnewline');
C = textscan(S, fmt);
This fails if there can be a # inside a string, or if there is a syntax for "escaping" # so they are not comment symbols, such as \#
apple = 12
peptalk = Apple, we are you #1 fans!
0 Comments
See Also
Categories
Find more on String Parsing 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!