MATLAB Answers

sort X and Y columns according to a repeated string in a 3rd column and scatter plot

2 views (last 30 days)
cgenes
cgenes on 12 Oct 2016
Commented: Guillaume on 13 Oct 2016
Hi, I have 3 columns (in a struct array called "data") columns are; "subject", "timepoint", "perimeter" . How can can I use something like gscatter to group the "person" column (there are 8 people) - I can get it to work for one person but am having trouble making it work for all 8 people . Thanks
x =[]; y = [];
for i = 1:length(data)
if strcmp(data(i).subject,'Person1')
x(i) = data(i).timepoint;
y(i) = length(data(i).perimeter);
end
end
scatter(x, y);
  4 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 12 Oct 2016
Edited: Guillaume on 12 Oct 2016
gscatter([data.timepoint], [data.perimeter], {data.subject})
But, I would recommend you convert you struct array into a table which is easier to work with:
tdata = struct2table(data);
gscatter(tdata.timepoint, tdata.perimeter, tdata.subject)
edit: So it seems you just want to sort the data and actually don't care about a scatter plot. Again, this is much easier if you convert to a table:
tdata = struct2table(data);
tdata = sortrows(data, 'subject');
With a structure:
[~, order] = sort({data.subject});
sorteddata = data(order);
  6 Comments
Guillaume
Guillaume on 13 Oct 2016
"For N groups, every integer between 1 and N must occur at least once in the vector of group numbers"
That's a limitation imposed by splitapply that I wasn't aware of. So, yes my method is not going to work.
Splitting the table in two
isyoung = contains(tdata.subject, 'Y');
tyoung = tdata(isyoung, :);
told = tdata(~isyoung, :);
and doing findgroup and splitapply on each is probably the simplest indeed.

Sign in to comment.

More Answers (1)

Massimo Zanetti
Massimo Zanetti on 12 Oct 2016
Edited: Massimo Zanetti on 12 Oct 2016
Get the "subject" column in one array containing data of all people, as follows:
allsubjects = data(:).subject
  3 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!