Clear Filters
Clear Filters

How to define the variable which contain multiple double data?

9 views (last 30 days)
Hello, I am not familar with MATLAB and I need advice from many experienced people.
I have multiple double arrays in my A structure (11x1 structure).
A structure have [?x2 double] format and I want to sort with 2nd column of all [A(1),A(2), ... A(11)] and merge all that sorted data in one variable.
I did it 1 by 1 and I got a data which I want, but it is such a long code and stupid way.
Does anyone have an idea how to summarize my code?
And my final goal is to define these 2 coulmns by x and y coordinates.
% Select n-th row of A and make double array and sort with 2nd column
A1 = vertcat(A(1).Groups); % size(A1) = 5 2
A1_1 = sortrows(A1,[2]);
A2 = vertcat(A(2).Groups); % size(A2) = 8 2
A2_1 = sortrows(A2,[2]);
A3 = vertcat(A(3).Groups); % size(A3) = 9 2
A3_1 = sortrows(A3,[2]);
A4 = vertcat(A(4).Groups); % size(A4) = 10 2
A4_1 = sortrows(A4,[2]);
A5 = vertcat(A(5).Groups); % size(A5) = 10 2
A5_1 = sortrows(A5,[2]);
A6 = vertcat(A(6).Groups); % size(A6) = 10 2
A6_1 = sortrows(A6,[2]);
A7 = vertcat(A(7).Groups); % size(A7) = 10 2
A7_1 = sortrows(A7,[2]);
A8 = vertcat(A(8).Groups); % size(A8) = 10 2
A8_1 = sortrows(A8,[2]);
A9 = vertcat(A(9).Groups); % size(A9) = 8 2
A9_1 = sortrows(A9,[2]);
A10 = vertcat(A(10).Groups); % size(A10) = 6 2
A10_1 = sortrows(A10,[2]);
A11 = vertcat(A(11).Groups); % size(A11) = 1 2
A11_1 = sortrows(A11,[2]);
% Merge all sorted A(n) in one variable
CenA1 = [A1_1;A2_1;A3_1;A4_1;A5_1;A6_1;A7_1;A8_1;A9_1;A10_1;A11_1];
% Separate these sorted coulmns by x and y coordinate
xcen1 = CenA1(:,1);
ycen1 = CenA1(:,2);

Accepted Answer

dpb
dpb on 4 Aug 2019
You were almost there...since each element in the struct array has just 2 columns, they can be catenated vertically--so convert the struct to an array like you were doing except do it globally...
XY=vertcat(A.Groups);
Now if it is needed to actually sort within the individual arrays, use a loop to sort first...unfortunately, structfun won't work on a struct array and arrayfun expects every array to be same size so have to write the explicit loop here...but that's not hard--
for i=1:numel(A)
A(i).Groups=sortrows(A(i).Groups,2);
end
and first, then catenate the result.
  1 Comment
Jihye Jung
Jihye Jung on 4 Aug 2019
Wow..Thank you so much.
I also thought to use for loop for sortrows of A, but I got some errors because each structure array has not same size!
I got a correct data with your help. Thank you and have a wonderful day.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!