フィルターのクリア

How to get 2 variables form loading mat fille

1 回表示 (過去 30 日間)
Rita
Rita 2016 年 2 月 5 日
コメント済み: Star Strider 2016 年 2 月 5 日
I want to load lots of mat file like (a1_2.mat......a50_40.mat) and take two variables(c,d) from each mat file and then put variables in a matrix like this.[c1 d1; c2 d2.....]
for n = 1 : 50
for k=1:40
y=load(['a',num2str(n),'_', num2str(k) '.mat']);
c = y.c;% load first variable
d=y.d; % load second variable and
all(:,1)=c;
all(:,2)=d;
end
end
but I can't get the final matrix correctly.
Thanks in advance for your help.

採用された回答

Walter Roberson
Walter Roberson 2016 年 2 月 5 日
for n = 1 : 50
for k = 1:40
y = load(['a',num2str(n),'_', num2str(k) '.mat']);
c = y.c;% load first variable
d = y.d; % load second variable and
all_vals(n,k,1) = c;
all_vals(n,k,2) = d;
end
end
all_vals = reshape(all_vals, [], 2);

その他の回答 (1 件)

Star Strider
Star Strider 2016 年 2 月 5 日
Assuming that you cannot pre-allocate your matrix, something like this could work:
all_vars = [];
for n = 1 : 50
for k=1:40
y=load(['a',num2str(n),'_', num2str(k) '.mat']);
c = y.c;% load first variable
d = y.d; % load second variable and
all_vars = [all_vars; [c d]];
end
end
I changed the name of your matrix, because all is a function name you could need later in your code. Naming variables the same as built-in MATLAB functions is called ‘overshadowing’. MATLAB will use the variable instead of the function afterwards, making the function useless until you change the variable name.
  2 件のコメント
Rita
Rita 2016 年 2 月 5 日
Thanks Walter for your prompt response.unfortunetly my results had 30 rows of zero number. Thanks Star strider for your explanation.and the result is perfect.
Star Strider
Star Strider 2016 年 2 月 5 日
My pleasure.
I am glad I was able to solve your problem!

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by