Ignoring comments in lines when parsing

조회 수: 5 (최근 30일)
g
g 2019년 5월 9일
답변: Walter Roberson 2019년 5월 9일
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!

답변 (2개)

Adam Danz
Adam Danz 2019년 5월 9일
편집: Adam Danz 2019년 5월 9일
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' }

Walter Roberson
Walter Roberson 2019년 5월 9일
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!

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by