Ignoring comments in lines when parsing
조회 수: 8 (최근 30일)
이전 댓글 표시
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
답변 (2개)
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' }
댓글 수: 0
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!