Skipping the lines in a .txt file before importing it

4 次查看(过去 30 天)
I am writing a code for manipulating the data in a .txt file and there are some lines before and after the file that i need to cut out since they are inconsequential. I am just in the beginning phases of this problem and i am just improvising as of now. My end goal is to be able to extract the calculated reaction times(ideally, in an excel file) and nothing else, not the raw reaction times. Here is what i have come up with. I thought i would start coding with some basics and see how that could be improved upon . Here is what i have down so far. This is very rudimentary so please feel free to advise/criticize the code.Any help would be greatly appreciated.
[filename] = uigetfile('*', 'Select the data file')
fid = fopen(filename,'r')
scan_init = cell2mat(textscan(fid, 'f %f %f %f', 'headerlines',19))
fclose(fid)
I am attaching the file in question alongside it.

采纳的回答

Walter Roberson
Walter Roberson 2016-1-8
Using headerlines is fine.
You missed a '%' at the beginning of your format.
You could use 'CollectOutput', 1 to avoid having to do the cell2mat:
scan_init_cell = textscan(fid, '%f %f %f %f', 'headerlines', 19, 'CollectOutput', 1);
scan_init = scan_init_cell{1};
  1 个评论
Walter Roberson
Walter Roberson 2016-1-8
Your file turns out to need 20 header lines, and 3 values per line instead of 4.
Also, the free-form text in the last column causes problems. Fortunately it is always the last column so we do not need to try to find a trailing valid numeric value: we can just read everything from the third column to the end of the line as a string, and then later ask to convert the string to numeric form. str2double() will notice the text and return NaN for those entries.
scan_init_cell = textscan(fid, '%f %f %[^\n]', 'headerlines', 20);
scan_init = [scan_init_cell{1}, scan_init_cell{2}, str2double(scan_init_cell{3})];

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by