How can I dynamically extract fields from a structure and create variables in the MATLAB workspace using the field names?

78 views (last 30 days)
I have a created a structure containg fields a, b and c. Now I want to decompose my structure and save a, b and c as workspace variables with the filed name as the variable name.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Jan 2010
One possibility to dynamically decompose your struct would be using FIELDNAMES command to determine the fields and create according variables using EVAL, like in the following example:
constants.a = 4;
constants.b = 8;
constants.c = 10;
names = fieldnames(constants);
for i=1:length(names)
eval([names{i} '=constants.' names{i} ]);
end
In case of nested structures, you can use nested FOR loops to access the fields.
constants.a.aa=4;
constants.a.ab = 5;
constants.b = 8;
constants.c = 10;
names = fieldnames(constants);
for i=1:length(names)
if isstruct(eval(['constants.' names{i}]))
names2 = fieldnames(eval(['constants.' names{i}]));
for j=1:length(names2)
eval([names{i} '_' names2{j} '=constants.' names{i} '.' names2{j}]);
end
else
eval([names{i} '=constants.' names{i} ]);
end
end
  3 Comments
Stephen23
Stephen23 on 7 Mar 2019
Edited: Stephen23 on 7 Mar 2019
@Dan Goldwater: ask this as its own question, rather than hijacking a thread from January 2010. Then you are likely to get people to help you.
In your question please explain exactly what the problem is, e.g. "..I need to be able to change whether 'speed' is stored in 'x' or in 'y'" : currently neither of x or y contain the field speed, so it is not clear how this is relevant or how it can be a problem. At some point in your code you will have to know where the values come from and where they need to be allocated to.
Steven Lord
Steven Lord on 7 Mar 2019
So you're trying to optimize a function of both length and time and depending on the specific optimization you want to fix length and optimize the best time or fix time and optimize the best length? Something like this might help (untested code):
function value = extractSpecifiedParameter(paramStruct, constStruct, paramName)
isparam = isfield(paramStruct, paramName);
isconst = isfield(constStruct, paramName);
if isparam && isconst
error('%s cannot be both a parameter and a constant.', paramName);
elseif isparam
% we know isconst is false or it would have been caught above
value = paramStruct.(paramName);
elseif isconst
% we know isparam is false or it would have been caught above
value = constStruct.(paramName);
else
% both isparam and isconst are false
error('%s is neither a parameter nor a constant.', paramName);
end
end
Call this like:
function z = fun(x,y)
%Block in which I extract variables from x and y, such as
lengthParameter = extractSpecifiedParameter(x, y, 'length');
time = extractSpecifiedParameter(x, y, 'time');
I changed the name of your first parameter variable because length already has a meaning in MATLAB.
You could add a second output to the declaration and construct that output from isparam and isconst to specify whether the parameter was in the paramStruct or constStruct if that information is useful for your function.

Sign in to comment.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Products


Release

R2008b

Community Treasure Hunt

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

Start Hunting!