Ignoring comments in lines when parsing
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Adam Danz
le 9 Mai 2019
Modifié(e) : Adam Danz
le 9 Mai 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 commentaires
Walter Roberson
le 9 Mai 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 commentaires
Voir également
Catégories
En savoir plus sur String Parsing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!