How do you make multiple variable assignments from s struct?
10 views (last 30 days)
Show older comments
There has to be a straightfoward way to do this but I can't find it to save my life:
s = struct([]);
s(1).x = 1;
s(1).y = 2;
s(2).x = -1;
s(2).y = -2;
[x1, y1] = s(1); % where x1 is initialized to be s(1).x and y1 to be s(1).y
I have a struct with >10 elements. I'd really like to not have to assign variables one line at a time.
0 Comments
Accepted Answer
Robert Cumming
on 4 Oct 2014
If you choose to do it the way Joseph Chang mentioned - then I would strongly advise that you change the eval statement to use dynamic fields instead, e.g.:
function a = extractfield(s,name)
a = s.(name)
end
You could remove the sub function entirely:
function varargout = extractfields(s)
name = fieldnames(s);
for ind = 1:length(name)
varargout{ind} = s.(name{ind});
end
end
0 Comments
More Answers (6)
Guillaume
on 4 Oct 2014
An even simpler implementation of the function, using structfun:
function varargout = extractfields(s)
varargout = structfun(@(f) {f}, s); %force output to cell array with {}
end
Steven Lord
on 29 May 2019
Making variables x1, x2, x3, ... x10 and y1, y2, ... y10 this way is discouraged. If your struct array had 1000 elements, do you really want two thousand individual variables in your workspace? What if it had 10000?
Instead, I recommend making two variables, one for x and one for y.
s = struct([]);
s(1).x = 1;
s(1).y = 2;
s(2).x = -1;
s(2).y = -2;
x = [s.x];
y = [s.y];
Wherever you would have referred to (say) x2, instead refer to x(2). It's a few more keystrokes, but a less cluttered and easier to manage workspace.
0 Comments
Azzi Abdelmalek
on 3 Oct 2014
Why do you need to create all these variables. read this http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
0 Comments
Sean de Wolski
on 3 Oct 2014
Not easily. If you want to do this for readability, then you're best off just unpacking them manually since that will be readable. Other workarounds involve kludge.
0 Comments
Joseph Cheng
on 3 Oct 2014
Edited: Joseph Cheng
on 3 Oct 2014
I completely understand trying to have readability for an equation. However instead, put a nice commented note in before or after to show the readable equation and how it relates to the messier one.
to do what you ask create one mfile called extractfields.m and paste below
function varargout = extractfields(s)
name = fieldnames(s);
for ind = 1:length(name)
varargout{ind} = extractfield(s,name{ind});
end
end
function a = extractfield(s,name)
a = eval(['s.' name]);
end
so as i placed all the arguments in varargout you just put the number of [x y z t a b c] or less depending on how many you want. The extractfield is actually a name of a function in the mapping toolbox that does the same thing. http://www.mathworks.com/help/map/ref/extractfield.html so if you have the mapping you probably don't need to use the one i put as another function it or incorperate it in the extractfields() function. I didn't just implement the varargout{ind}=eval(_) incase you have the function and i don't like using eval unless necessary.
s = struct([]);
s(1).x = 1;
s(1).y = 2;
s(2).x = -1;
s(2).y = -2;
[x1 y1] = extractfields(s(1));
[x2 y2] = extractfields(s(2));
notes: you need to know the extent of how many items you're extracting and the order, not knowing would cause missing data or an error. I didn't have time to implement something fancy to auto create for you the [x# y#]. But shouldn't be too difficult to read in s, find the number of structs (1:n) then append the fieldnames with that number in the above.
0 Comments
See Also
Categories
Find more on Structures 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!