How to Convert a Scalar Struct with Vector Fields to a Vector Struct with Scalar Fields?
I have a scalar struct S with each field a row vector of the same number of elements.
S.x = [1 2];
S.y = [10 20];
I want create a struct array, A, with same number of elements as in the fields in S, with the same field names as S, where each field value in each element of A is correspondingly indexed from the original field in S. I’m not sure I’m explaining this well.
So far I have this, which works:
C = [fieldnames(s) , arrayfun(@(c) mat2cell(c{1},1,ones(1,numel(c{1}))),struct2cell(s),’Uni’,false)].’;
A = struct(C{:})
[A.x]
[A.y]
Open to suggestions on better (e.g., easier to understand) alternatives.I have a scalar struct S with each field a row vector of the same number of elements.
S.x = [1 2];
S.y = [10 20];
I want create a struct array, A, with same number of elements as in the fields in S, with the same field names as S, where each field value in each element of A is correspondingly indexed from the original field in S. I’m not sure I’m explaining this well.
So far I have this, which works:
C = [fieldnames(s) , arrayfun(@(c) mat2cell(c{1},1,ones(1,numel(c{1}))),struct2cell(s),’Uni’,false)].’;
A = struct(C{:})
[A.x]
[A.y]
Open to suggestions on better (e.g., easier to understand) alternatives. I have a scalar struct S with each field a row vector of the same number of elements.
S.x = [1 2];
S.y = [10 20];
I want create a struct array, A, with same number of elements as in the fields in S, with the same field names as S, where each field value in each element of A is correspondingly indexed from the original field in S. I’m not sure I’m explaining this well.
So far I have this, which works:
C = [fieldnames(s) , arrayfun(@(c) mat2cell(c{1},1,ones(1,numel(c{1}))),struct2cell(s),’Uni’,false)].’;
A = struct(C{:})
[A.x]
[A.y]
Open to suggestions on better (e.g., easier to understand) alternatives. struct MATLAB Answers — New Questions