Count the number of space character from a text file which has multiple lines
Show older comments
Hello everyone,
I am currently working on my function of counting space characters, it works well if my text file has only 1 line but it messes up when i input multiples lines into my file. Could anyone please tell me what I am doing wrong?
function space = swap(file)
file = 'mytext.txt';
f = fopen(file, 'rt');
while feof(f) == 0
s = fgetl(f);
a = length(strrep(s, ' ', ''));
b = length(s);
space = b - a;
end
end
1 Comment
Walter Roberson
on 7 Jun 2019
You are overwriting space for each input line.
Accepted Answer
More Answers (0)
Categories
Find more on Single-Rate Filters 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!