how to count for whitespace symbol occurrences in a .txt file?

7 views (last 30 days)
i found a way to read the .txt file and count the symbols using this code:
v=textread('FF7.m','%c'); count=length(strfind(v,'(symbol)'));
but i cant find a way to count the whitespaces, space, breaks, or tabs. in addition, since the input should be in script, i cant put the single quotation in the script. i also need to count the ' symbol.
can someone help me? any help will be very appreciated. thank you in advance!

Accepted Answer

Cedric
Cedric on 9 Mar 2013
Edited: Cedric on 9 Mar 2013
Alternatively, you can use a simple regexp. It will certainly not be the most efficient solution though for this simple task:
content = fileread('FF7.m') ;
count = numel(regexp(content, '\s')) ;
EDIT: for counting ' only, you can do
count = sum(content == '''') ;
If you wanted to count/sum whitespaces and ' with a single regexp call, you could do the following:
count = numel(regexp(content, '[\s'']')) ;

More Answers (1)

Walter Roberson
Walter Roberson on 9 Mar 2013
'''' is a quoted single-quote.
You may wish to consider using ismember().
Also,
newline = sprintf('\n');
cr = sprintf('\r');
tab = sprintf('\t');

Community Treasure Hunt

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

Start Hunting!