reading selected columns of multiple files and storing the data

9 次查看(过去 30 天)
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 个评论
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.

请先登录,再进行评论。

类别

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