How do you make multiple variable assignments from s struct?
조회 수: 10 (최근 30일)
이전 댓글 표시
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
채택된 답변
Robert Cumming
2014년 10월 4일
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
추가 답변 (6개)
Guillaume
2014년 10월 4일
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
2019년 5월 29일
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
Azzi Abdelmalek
2014년 10월 3일
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
Sean de Wolski
2014년 10월 3일
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
Joseph Cheng
2014년 10월 3일
편집: Joseph Cheng
2014년 10월 3일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!