Can I dynanically reference a variable name, similar to dynamic field references?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
K E
el 13 de Mayo de 2014
Comentada: K E
el 13 de Mayo de 2014
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
0 comentarios
Respuesta aceptada
Walter Roberson
el 13 de Mayo de 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');
Más respuestas (0)
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!