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

조회 수: 4 (최근 30일)
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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by