Count the number of space character from a text file which has multiple lines
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nobita Nobi
el 7 de Jun. de 2019
Respondida: Debasish Samal
el 7 de Jun. de 2019
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 comentario
Respuesta aceptada
Debasish Samal
el 7 de Jun. de 2019
What you are actually doing in the while loop is , assigning the number of spaces in each line to the variable space. It will always store the number of spaces in the last line at the end of the loop. If you want to calculate total number of spaces do
space = space + (b-a);
It will look something like this:
file = 'abc.txt';
f = fopen(file, 'rt');
space = 0;
while feof(f) == 0
s = fgetl(f);
a = length(strrep(s, ' ', ''));
b = length(s);
space = space + (b - a);
end
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Axis Labels en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!