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

4 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 13 May 2014
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');

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!