reading selected columns of multiple files and storing the data

4 ビュー (過去 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 Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by