How can I remove all spaces in a .txt file ?
Show older comments
fid = fopen('test_hex.txt','r');
tline = fgetl(fid);
while ischar(tline)
disp(tline);
tline = fgetl(fid);
end
fclose(fid);
Here is my code to read txt file. I want to delete all spaces. How can I do it ? I have attached txt file
1 Comment
Brendan Hamm
on 15 Jan 2016
Inside your loop you can just write:
formatLine = regexprep(tline,' ','');
which will replace all spaces with the empty string. Now just decide how you want to store the data as this would overwrite the previous iteration everytime (maybe use a counter to store this outputto the kth row of a char array)
Answers (1)
If you really mean all as in "all"...
>> type burak.txt % made a file with your line twice...
08 0C F0 6D 86 EA 44 2D
08 0C F0 6D 86 EA 44 2D
>> fid=fopen('burak.txt','rt'); % open as text file to handle EOL
>> c=fread(fid,inf,'*char').'; % read into character array, transpose
c =
08 0C F0 6D 86 EA 44 2D
08 0C F0 6D 86 EA 44 2D
>> c(c==' ')=''; % remove blanks
c =
080CF06D86EA442D
080CF06D86EA442D
>> fid=fclose(fid);
Alternatively, use could still use strrep but for this purpose it's simpler to do the direct substitution.
c=strrep(c,' ','');
Categories
Find more on Large Files and Big Data 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!