Looping through different files with sprintf

11 ビュー (過去 30 日間)
Dhea Bengardi
Dhea Bengardi 2020 年 7 月 1 日
コメント済み: Rik 2020 年 7 月 1 日
Hi, I am trying to run some data analysis on data from an experiment and the different participant results are stored in separate MAT files. The data starts from sub01.mat -> sub13.mat. With teh code above, I get an error as it starts the file name at sub1.mat instead of sub01.mat. Any suggestions?

採用された回答

Rik
Rik 2020 年 7 月 1 日
Read the documentation for sprintf.
filename=sprintf('sub%02d.mat',s);
You also probably want to actually the loaded data:
ind=S.outDat(:,i)==k;
% ^^

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2020 年 7 月 1 日
Hello
I see you get already an answer
otherwise you can also do the same loop using dir
example attached
my solution works even if the data are stored in a different variable name for each mat file
all the best
  1 件のコメント
Rik
Rik 2020 年 7 月 1 日
There are several problems with your code:
  • Why use clear all? Even clear itself is not really needed if you don't make any typos. clear all should appear exactly once in your entire codebase.
  • Why use close all? You aren't opening any figures or plotting anything, so why close all figures? Maybe there is a figure open with usefull data, or even a GUI.
  • Why use eval? You can simply loop through the fieldnames: data=MyStruct.(fn{n});. No eval required.

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

カテゴリ

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