Use of fopen,fscanf and fclose.

조회 수: 4 (최근 30일)
Massilon Toniolo da Silva
Massilon Toniolo da Silva 2017년 5월 21일
댓글: Walter Roberson 2017년 5월 22일
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
Walter Roberson
Walter Roberson 2017년 5월 21일
Is there a header line on the file? What does the first two lines look like?
dpb
dpb 2017년 5월 21일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2017년 5월 21일
Try this:
data = importdata('chf201.txt');
column2 = data(:, 2);

Massilon Toniolo da Silva
Massilon Toniolo da Silva 2017년 5월 21일
Dear Stephen Cobeldick,
I am sorry for the delay. Now I am sending the files annotations_nsr001 and annotations_chf201.txt both from Physionet.org (Normal Sinus Rhythm RR Intervals Database). I thank you very much for any help. Kind regards, Massilon
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 5월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by