MATLAB textscan headerlines

烈酒焚心 提交于 2021-02-16 09:15:11

问题


When I try to use headerlines with textscan to skip the first line of the text file, all of my data cells are stored as empty.

fid = fopen('RYGB.txt');
A = textscan(fid, '%s %s %s %f', 'HeaderLines', '1'); 
fclose(fid);

This code gives

1x4 Cell
[] [] [] []

Without the headerlines part and without a first line that needs to be skipped in the text file, the data is read in with no problem. It creates a 1x4 cell with data cells containing all of the information from the text file in columns.

What can I do to to skip the first line of the text file and read my data in normally?

Thanks


回答1:


I think your problem is that you have specified a string instead of an integer value for HeaderLines. The character '1' is interpreted as its ASCII value, 0x31 (49 decimal), so the first 49 lines are skipped. Your file probably contains 49 lines or less, so everything ends up being discarded. This is why you're getting empty cells.

The solution is to replace '1' with 1 (i.e. remove the quotes), like so:

A = textscan(fid, '%s %s %s %f', 'HeaderLines', 1);

and this should do the trick.



来源:https://stackoverflow.com/questions/14172563/matlab-textscan-headerlines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!