issue regarding reading .txt file with comments

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

Walter Roberson
Walter Roberson 2018 年 8 月 1 日
How are the comments indicated in the text?
KSSV
KSSV 2018 年 8 月 1 日
A snippet of file or attaching the file will help us to help you.
Akshat Shrivastava
Akshat Shrivastava 2018 年 8 月 1 日
I am adding a screenshot of the code. As you can see, on row 7 and 24 i have added a comment. but the program is only running till row 6 and not after that. can you help with a code which will run all the rows (including the comment)

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

 採用された回答

Adam Danz
Adam Danz 2018 年 8 月 1 日
編集済み: Adam Danz 2018 年 8 月 1 日

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
Walter Roberson 2018 年 8 月 1 日
You would probably want
C = cell2mat( textscan(fid, '%f%f', 'CommentStyle', '#', 'CollectOutput', true) );

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

その他の回答 (1 件)

Stephen23
Stephen23 2018 年 8 月 2 日
編集済み: Stephen23 2018 年 8 月 3 日

0 投票

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

Akshat Shrivastava
Akshat Shrivastava 2018 年 8 月 2 日
Thank you so much for the help!! It worked :)
Akshat Shrivastava
Akshat Shrivastava 2018 年 8 月 3 日
Hi, Just have a query with the syntax you told. After using it, I am able to plot the graph, but as per the numerical data in text file it is not corresponding the correct values. I have attached the screenshot of the plot and the text file data. For example: in the text file. the time at 259 second corresponds to -0.0998, but as per the plot it is something else. Can you help me this. Thanks
Stephen23
Stephen23 2018 年 8 月 3 日
編集済み: Stephen23 2018 年 8 月 3 日
@Akshat Shrivastava: please upload your data file. I cannot load a screenshot as data.
Please show the code that you used. If you do not show the code you used than I have to guess or imagine what you are doing. Guessing is not a very accurate way to debug code!
Akshat Shrivastava
Akshat Shrivastava 2018 年 8 月 3 日
@ Stephen Cobeldick. I have used the same code as mentioned above. (The one which you told)
Stephen23
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
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.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by