how to load set of files into different variables and take specific column of those files and make a matrix

1 回表示 (過去 30 日間)
S
S 2018 年 2 月 2 日
編集済み: S 2018 年 2 月 3 日
Hi,
I have 10 files where each has 7 columns. I want to load 4th column of each file into a matrix and plot it with the 1st column. Since 1st column is same for all, have to load 1st column from a single file. I tried with 'load' but can't assign file to a variable with 'load'
Thank you
  3 件のコメント
S
S 2018 年 2 月 2 日
They are from a different simulation software, but they are .mat files.
S
S 2018 年 2 月 2 日
編集済み: S 2018 年 2 月 2 日
I tried with
files = dir('*.mat'); for i=1:length(files) eval(['load ' files(i).name]); end but cannot assign to different variables with 'eval'
Also tried X(i) = sprintf('X_emi%d.mat ',i); but didn't work

サインインしてコメントする。

回答 (1 件)

Bob Thompson
Bob Thompson 2018 年 2 月 2 日
If it is possible to open them with a text editor you could use the textread() command. This is designed to read ascii files, so some characters may not be able to be loaded, but it should read any numbers or regular strings just fine.
Additionally, you can use uigetfile() to allow the selection of the files through a UI:
[filename,filedir] = uigetfile('*.mat','Multiselect','on');
This will pull all of your files into a matrix where you can then use your for loop to run through each file. After you select the file with the for loop just load the data into a matrix with textread() and put the data you want into a different array for storage.
for I = 1:length(filename);
data = textread(filename(I),'headerlines',1);
if I == 1;
constant = data(:,1);
end
storage(:,1) = constant;
storage(:,I+1) = data(:,4);
end
After the code has read through all of the files you will be left with an array, storage, which contains the first column as the constant, and the other columns as the fourth column of each file. You can organize storage however you would like, but this is a sample of the concept.
  2 件のコメント
S
S 2018 年 2 月 3 日
Thank you very much. I really appreciate your response. I'll try this.
S
S 2018 年 2 月 3 日
編集済み: S 2018 年 2 月 3 日
I had to change textread to textscan and I got following error.
Error using textscan First input must be a valid file-id or non-empty character vector.
Error in untitled10 (line 4) data = textscan(filename(I),'headerlines',1);
Could you please tell me what it means.
What's headerlines means here?
thank you

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by