Can I dynanically reference a variable name, similar to dynamic field references?

4 ビュー (過去 30 日間)
K E
K E 2014 年 5 月 13 日
コメント済み: K E 2014 年 5 月 13 日
I have a large mat file containing many different variables. I would like to load and process the variables by working through a list of their names. Is there a way other to do this than eval, perhaps similar to dynamic field references as shown in the non-working example below? Saving the variables as fields isn't an option, because the resulting structure is too big to load, and I don't need all the variables at once.
listOfVars = {'dog', 'cat', 'hamster', 'budgie'}; % Example big variables in the file
for iVar = [1 3] % Only load and process some of the variables
load('bigFile.mat', listOfVars{iVar}); % Load a single variable
% Manipulate the variable using a dynamic reference to its name. Doesn't work, of course.
plot((listOfVars{iVar}).weight, (listOfVars{iVar}).foodConsumed, 'x');
hold on;
end

採用された回答

Walter Roberson
Walter Roberson 2014 年 5 月 13 日
You should be able to use the command form of load together with dynamic field names.
thisdata = load('bigFile.mat', listOfVars{iVar});
plot(thisdata.(listOfVars{iVar}).weight, thisdata.(listOfVars{iVar}).foodConsumed, 'x');

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by