Access structure arrays after load from a for loop

1 回表示 (過去 30 日間)
Jon
Jon 2017 年 10 月 3 日
コメント済み: Jon 2017 年 10 月 3 日
I'm trying to analyze multiple sequential files (e.g. fv1, fv2, fv3, etc) using a for loop. However, when loading, it adds the variable as another field name (i.e. d.fv1.field.field- see below).
for i=1:5;
d=load(['fv' num2str(i) '.mat']);
data(i,:)=d.Y(4).Data;
If I try to access the data I want, I can't because: 1. Its added a new field name 2. The original file name is still included but I can't access it with structure accessing
for i=1:5;
d=load(['fv' num2str(i) '.mat']);
data(i,:)=d.strcat('fv' num2str(i)).Y(4).Data;
I'm relatively new to Matlab, and while similar questions may have answered parts of this I haven't found anything that addresses this directly. Is this something I'll have to tackle with dynamic variables (which I have almost no grasp on). Thanks so much for any help!

採用された回答

Walter Roberson
Walter Roberson 2017 年 10 月 3 日
for i = 1:5
fn = sprintf('fv%d', i);
d = load( [fn '.mat']);
data(i,:)=d.(fn).Y(4).Data;
end
  1 件のコメント
Jon
Jon 2017 年 10 月 3 日
Thanks, it worked great!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by