Use of fopen,fscanf and fclose.
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Dear colleagues,
I have been trying to select rows and columns of a data set in text file. The data I have got is displayed as a 90-by-4 matrix. I would need to have a matrix consisting of all rows but only column 2. The way I have tried to carry it out is as follows:
fid = fopen('chf201.txt','r');
data=fscanf(fid,'%g', [4,inf]);
fclose(fid);
result=data(2,:);
result =
0
Attempted to access data(2,:); index out of bounds because numel(data)=1.
Could you please help me in this matter, since I did not get the 90-by-1 matrix as the result? Kind regards, Massilon
3 Commenti
Walter Roberson
il 21 Mag 2017
Is there a header line on the file? What does the first two lines look like?
dpb
il 21 Mag 2017
Besides whatever is wrong in your attempt to read the data (probably something like what Walter has suggested would be a very good guess),
result=data(2,:);
would be the 2nd row, all columns, not the second column.
Try importdata for the easy-peasy way to read a text file with header and see if it can infer the format automagically.
Risposte (2)
Image Analyst
il 21 Mag 2017
Try this:
data = importdata('chf201.txt');
column2 = data(:, 2);
0 Commenti
Massilon Toniolo da Silva
il 21 Mag 2017
1 Commento
Walter Roberson
il 22 Mag 2017
I recommend using readtable() for those, if you have R2013b or later. Especially if you have R2014b or later, readtable() will take care of creating datetime objects out of the first column.
Vedere anche
Categorie
Scopri di più su String Parsing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!