How can i remove all spaces from all lines in a text file?
Mostra commenti meno recenti
In a text file i want to remove all spaces from all the lines to create a file with the same lines , but without the spaces
Risposte (2)
the cyclist
il 24 Mag 2016
Modificato: the cyclist
il 24 Mag 2016
Here is one way:
fid_in = fopen('text_in.txt');
fid_out = fopen('text_out.txt','w');
text_in = fgetl(fid_in);
while ischar(text_in)
text_out = [regexprep(text_in,' ','')];
fprintf(fid_out,'%s\n',text_out);
disp(text_in)
disp(text_out)
text_in = fgetl(fid_in);
end
fclose(fid_in);
fclose(fid_out);
I tested it on the attached file.
Here is a fast solution in just three lines:
>> fid = fopen('text_out.txt','wt');
>> fprintf(fid,'%s',strrep(fileread('text_in.txt'),' ',''))
>> fclose(fid);
1 Commento
berkelb
il 25 Mag 2016
Categorie
Scopri di più su Characters and Strings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!