From Structs of Arrays:
s(1).x = [1 2 3; 4 5 6; 7 8 9];
s(2).x = [11 12 13; 14 15 16; 17 18 19];
I would like to get a matrix made up of, say, the second rows only - without using a loop
ans = [4 5 6; 14 15 16]
But typing
s.x(2,:) or [s.x(2,:)] or {s.x(2,:)}
gives the error
"Expected one output from a curly brace or dot indexing expression, but there were 2 results."
Any help appreciated. Jack

댓글 수: 2

Stephen23
Stephen23 2016년 2월 18일
편집: Stephen23 2016년 2월 18일
I am not sure that those attempted syntaxes would make much sense anyway...
s.x
actually creates a comma separated list, like this:
s(1).x, s(2).x, ... s(end).x
It makes no sense to apply indexing to the last variable in a comma separated list:
A,B,C,D(:,2)
and then expect that the indexing is applied to all variables within the comma separated list. This would be counter-intuitive. The only logical place would be after the square brackets/curly braces, in which case this becomes a standard application of indexing into a single temporary variable, like this:
[s.x](:,2)
This is an enhancement that many people would like, but so far has not yet been implemented.
Jack
Jack 2016년 2월 18일
Makes sense. I voted up Walter's proposal.

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2016년 2월 18일

2 개 추천

cell2mat( arrayfun(@(c) c.x(2,:), (1:length(s)).', 'Uniform', 0) )

댓글 수: 8

Jack
Jack 2016년 2월 18일
Thank you, I was hoping I had the wrong syntax but I see that Matlab simply can't do it.
Delyle Polet
Delyle Polet 2016년 8월 6일
편집: Delyle Polet 2016년 8월 7일
Nice, just missing a reference to s. Should be
cell2mat( arrayfun(@(c) c.x(2,:), s(1:length(s)).', 'Uniform', 0) )
Could also be written more simply as
cell2mat( arrayfun(@(c) c.x(2,:), s.', 'Uniform', 0) )
I wonder why I didn't code
cell2mat( arrayfun(@(c) c.x(2,:), s(:), 'Uniform', 0) )
Ben Oeveren
Ben Oeveren 2017년 10월 31일
편집: Guillaume 2017년 10월 31일
For everyone still interested. The following seems to do the job. But it is not specifically fast. It works even when you have a struct with multiple field layers. Just insert your desired string.
function m = struct2mat(s,fields)
% Convert a structure to a matrix.
% fields (string) - s(i).my1stfield.my2ndfield.my3thfield
% Ben van Oeveren,
m = [];
for i = 1:length(s)
mnew = eval(['s(i).' fields]);
m = [m;mnew];
end
end
Guillaume
Guillaume 2017년 10월 31일
편집: Guillaume 2017년 10월 31일
@Ben,
Please format your code properly.
it's not specifically fast and worse it uses eval when it's not needed at all.
m = [];
fieldlist = strsplit(fields, '.');
for i = 1:numel(s)
m = [m; getfield(s(i), fieldlist{:}));
end
It would be better to get the list of field names as a cell array of individual fields rather than as a char array of field names joined by dots as this would avoid the need for the strsplit line.
Note that your code, and Walter's first answer, only work properly with vector structures, due to the problematic use of length.
Stephen23
Stephen23 2017년 10월 31일
편집: Stephen23 2017년 10월 31일
Ben Oeveren
Ben Oeveren 2017년 10월 31일
Thank you good idea. Just tested the eval on timing. The difference in time is not really big.
For 106 fields, in total a double of 10883839x3 Elapsed time is 14.001423 seconds. eval Elapsed time is 13.644880 seconds. normal loop
Nevertheless, getfield and setfield seems like good alternative solution.
Stephen23
Stephen23 2017년 10월 31일
@Ben Oeveren: consider not just just the simple timing in a loop, but also that JIT does not work, debugging tools do not work, variable highlighting does not work, the security risk, etc.
And thinking that "my code does not need to be secure" is exactly why doing this is so easy:

댓글을 달려면 로그인하십시오.

Jan
Jan 2016년 2월 18일
편집: Jan 2016년 2월 18일

0 개 추천

Walter's suggestion is compact and nice. Internally this contains loops also. So the actual problem I would solve is this:
... without using a loop
Create a loop, care for a proper pre-allocation, export this to a secific M-function if you want to keep you main program clean and lean.

댓글 수: 1

Jack
Jack 2016년 2월 18일
I see. Braces would seem intuitive: would be nice to have such functionality in the future if possible...

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

질문:

2016년 2월 18일

댓글:

2017년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by