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 件のコメント

Brendan Hamm
Brendan Hamm 2016 年 1 月 15 日
Look at regexprep , literally translates to regular expression replacement.
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)

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2016 年 1 月 15 日
編集済み: dpb 2016 年 1 月 15 日

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,' ','');

2 件のコメント

Burak KTK
Burak KTK 2016 年 1 月 15 日
Thank you for your answer. Actually all means: i want to delete spaces in each line (line by line). For example: input: 08 0C F0 6D 86 EA 44 2D
output:080CF06D86EA442D
dpb
dpb 2016 年 1 月 15 日
So what's the difference in line-by-line and the whole file?
BTW, if on Windows need to tell fopen it's a text file and I neglected to transpose from native column to row-major order for display purposes....see updated Answer.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLarge Files and Big Data についてさらに検索

質問済み:

2016 年 1 月 15 日

編集済み:

dpb
2016 年 1 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by