Clear Filters
Clear Filters

how to filter column from a text file

1 view (last 30 days)
I combined two text files into one file.
and then i have to filter column.
For example,
12345 0 123 0.234 0.1234 0.1234 0.123
12345 1 123 0.234 0.1234 0.1234 0.123
12345 1 123 0.234 0.1234 0.1234 0.123
456 5 560 0.123 0.2345 0.5412 0.899
456 8 560 0.123 0.2345 0.5412 0.899
456 8 560 0.123 0.2345 0.5412 0.899
I need two columns skipped and fprintf again in the text file.
Like this,
12345 0.234 0.1234 0.1234 0.123
12345 0.234 0.1234 0.1234 0.123
12345 0.234 0.1234 0.1234 0.123
456 0.123 0.2345 0.5412 0.899
456 0.123 0.2345 0.5412 0.899
456 0.123 0.2345 0.5412 0.899
how can I do that?

Accepted Answer

Stephen23
Stephen23 on 28 Sep 2018
Edited: Stephen23 on 28 Sep 2018
fmt = '%-6d %.3f %.4f %.4f %.3f\n';
mat = dlmread('test_old.txt');
[fid,msg] = fopen('test_new.txt','wt');
assert(fid>=3,msg)
fprintf(fid,fmt,mat(:,[1,4:end]).')
fclose(fid);
Or simply use Notepad++: select those columns using shift+alt, then press delete.
  2 Comments
Hyo Wan Park
Hyo Wan Park on 28 Sep 2018
Thanks a lot!!!! it's so perfect answers to me.
Stephen23
Stephen23 on 28 Sep 2018
Another option would be to read the file using textscan, with string format specifiers. Then you could retain exactly the same character data that the first file contains.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!