issue regarding reading .txt file with comments
12 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to load a 'txt' file and plot it. It has just 2 columns and multiple rows. I also have few rows which have comments which i can not remove. But when i load the file and plot it. it just plot the data till the row which has the 1st comment. After that it is not acknowledging or reflecting the data. What can be done for this? Please help me out. I am using 2018 version Thanks in advance.
3 个评论
采纳的回答
Adam Danz
2018-8-1
编辑:Adam Danz
2018-8-1
If you are using textscan() to read in the data, you can use the 'CommentStyle' property which will ignore any text on a single line following the comment character ('#').
A simple example
fid = fopen('test1.txt');
C = textscan(fid, %f, 1, 'CommentStyle', '#');
fclose(fid);
1 个评论
Walter Roberson
2018-8-1
You would probably want
C = cell2mat( textscan(fid, '%f%f', 'CommentStyle', '#', 'CollectOutput', true) );
更多回答(1 个)
Stephen23
2018-8-2
编辑:Stephen23
2018-8-3
More robust:
opt = {'CommentStyle','#', 'CollectOutput',true};
[fid,msg] = fopen('yourfile.txt','rt');
assert(fid>=3,msg)
C = textscan(fid, '%f%f', opt{:});
fclose(fid);
M = C{1};
Or use dlmread.
6 个评论
Stephen23
2018-8-3
编辑:Stephen23
2018-8-3
Akshat Shrivastava: please upload your data file. I cannot load a screenshot as data.
Possibly you just need to add 'MultipleDelimsAsOne' to the options cell array, but without your actual data file this is impossible for me to test.
Or you might be able to trivially use dlmread. This would be easy to check if I had your data file.
Walter Roberson
2018-8-3
In particular we need to verify whether the file is tab delimited or simple space delimited or is fixed width fields.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!