Skipping the lines in a .txt file before importing it

4 vues (au cours des 30 derniers jours)
Sanwal Yousaf
Sanwal Yousaf le 8 Jan 2016
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.
  2 commentaires
Walter Roberson
Walter Roberson le 8 Jan 2016
Nothing was attached
Sanwal Yousaf
Sanwal Yousaf le 8 Jan 2016
appologies. DONE

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Jan 2016
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 commentaire
Walter Roberson
Walter Roberson le 8 Jan 2016
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})];

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by