Create a workspace with a given variable from multiple saved workspaces

8 ビュー (過去 30 日間)
AlisonS
AlisonS 2018 年 5 月 9 日
コメント済み: Stephen23 2018 年 5 月 10 日
Multiple participants have completed multiple trials of a task. Each trial is saved as a separate workspace. They are saved in the following format "Participant ID_Trial Number" (i.e. "ID_1", "ID_2", "ID_3", and so on). I want to load a specific variable ("var") from ID_1 and make it a variable "var_ID_1". And then I want to continue to do this for the same variable for all of the trials for all of the participants. I want to create a for loop to automatically do this for all files and then to save the new workspace containing "var" for all participants and all trials.
Thank you!!
  1 件のコメント
Stephen23
Stephen23 2018 年 5 月 10 日
"They are saved in the following format "Participant ID_Trial Number" (i.e. "ID_1", "ID_2", "ID_3", and so on). I want to load a specific variable ("var")..."
So you have lots of files named ID_1, ID_2, etc, and each contains a variable var. Sounds quite reasonable so far.
"... from ID_1 and make it a variable "var_ID_1" "
You should avoid doing this. Dynamically accessing variable names is how beginners force themselves into writing slow, complex, buggy, and hard to debug code. Read this to know more:
The simpler alternative would be to use one variable, e.g. with indexing (e.g. cell, ND numeric, etc) or dynamic row/fieldnames (e.g. struct, table, etc). In your case a simple non-scalar structure might be quite suitable (read the link to learn about the handy ways that you can access the contents of the non-scalar structure):
S(1).name = "ID XXX"
S(1).data = [...]
S(2).name = "ID YYY"
S(2).data = [...]
In your case you can do this quite easily using a loop:
S = dir('*.mat')
for k = 1:numel(S)
tmp = load(S(k).name);
Z(k).name = S(k).name;
Z(k).data = tmp.var;
end
Then you can easily get all of the names in a cell array:
names = {Z.name}
or easily loop over all data:
for k = 1:numel(Z)
Z(k).data
end
Better code through better data design!

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

回答 (1 件)

Rik
Rik 2018 年 5 月 9 日
You could use eval to do that, but you shouldn't. You should use indexing instead. Use a cell array or a struct.
(I have taken the liberty of adapting some of Stephen Cobeldick's thoughts on this topic) Introspective programing (eval and other functions like it) is slow, and the MATLAB documentation warns specifically against it. Using eval is slow, makes debugging difficult, and removes all code helper tools (code checking, syntax hints, code completion, variable highlighting, etc.). A longer discussion can be found here. If you are not an expert and you think you need eval, there are probably better solutions to your problem. Also, if you are not an expert, you might find interesting and helpful hints on this page (and if you are an expert, you can still learn from it).

カテゴリ

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