reading selected columns of multiple files and storing the data

7 views (last 30 days)
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 Comments
Sumera Yamin
Sumera Yamin on 4 Jun 2018
i see now. Somehow original file became corrupt. sorry for that mistake. I have attached the file again. hope its ok now.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 4 Jun 2018
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 Comments
Walter Roberson
Walter Roberson on 4 Jun 2018
Then in this code, yourdata(:,1) and yourdata(:,2) are the two things you want.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!