reading selected columns of multiple files and storing the data

조회 수: 2 (최근 30일)
Sumera Yamin
Sumera Yamin 2018년 6월 4일
댓글: Sumera Yamin 2018년 6월 6일
Hi, I have a simple problem but as a beginner stuck on it. I want to read 10 files in matlab with 3 columns in each file. I want to read last two columns of each file, store the values of each column individually in a column vector (to be used later on in code). In this way i will have 10 column vectors. What would be the best way to do it. Any help will be highly appreciated. My idea is to read one file and store the column and then apply for loop. But reading one file, as shown in my code below gives the output as reading data of all three columns and printing them in the form of an array.
fid = fopen('C:\Users.......file_ex.txt','rt'); % ..... shows path for the file location
indata = textscan(fid, '%f', 'HeaderLines',0);
fclose(fid);
yourdata = indata{1}
the output is as shown below.
yourdata =
0.0500
0.0010
0.0020
0.0100
0.0015
0.0023
0.0150
0.0029
0.0026
0.0200
0.0026
0.0024
0.0250
0.0069
0.0051
0.0300
0.0054
0.0056
0.0350
0.0035
0.0029
0.0400
0.0069
0.0058
the original file is as attached. I will be grateful if anyone could help.
  댓글 수: 5
jonas
jonas 2018년 6월 4일
use
readtable('file_ex.txt')
instead of textscan
Sumera Yamin
Sumera Yamin 2018년 6월 4일
i see now. Somehow original file became corrupt. sorry for that mistake. I have attached the file again. hope its ok now.

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

답변 (1개)

Walter Roberson
Walter Roberson 2018년 6월 4일
yourdata = cell2mat( textscan(fid, '%*f%f%f', 'CollectOutput', 1) );
"store the values of each column individually in a column vector (to be used later on in code). In this way i will have 10 column vectors"
That part is not clear. In the case of the sample file which contains 8 rows, do you want all of column 2 followed by all of column 3? Or do you want two different outputs, one for each column?
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 6월 4일
Then in this code, yourdata(:,1) and yourdata(:,2) are the two things you want.
Sumera Yamin
Sumera Yamin 2018년 6월 6일
Thank you very much for your time and help.

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by