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

6 ビュー (過去 30 日間)
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 件のコメント
cgenes
cgenes 2016 年 10 月 12 日
編集済み: cgenes 2016 年 10 月 12 日
OMG - just found the unique() function - - boom!

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2016 年 10 月 12 日
編集済み: Guillaume 2016 年 10 月 12 日
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 件のコメント
cgenes
cgenes 2016 年 10 月 13 日
Hi thanks very much for this - - the new columns are made if i try plotting the logical way it says ....
Error using splitapply (line 108) For N groups, every integer between 1 and N must occur at least once in the vector of group numbers.
and the logical way it says . . .
Error using splitapply (line 96) The data variables must have the same number of rows as the vector of group numbers. The group number vector has 37 row(s), and data variable 1 has 19 row(s).
thanks for all your help on this by the way!! I think i'll just create two new tables one for young and one for old
Guillaume
Guillaume 2016 年 10 月 13 日
"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.

サインインしてコメントする。

その他の回答 (1 件)

Massimo Zanetti
Massimo Zanetti 2016 年 10 月 12 日
編集済み: Massimo Zanetti 2016 年 10 月 12 日
Get the "subject" column in one array containing data of all people, as follows:
allsubjects = data(:).subject
  3 件のコメント
Guillaume
Guillaume 2016 年 10 月 12 日
編集済み: Guillaume 2016 年 10 月 12 日
data(:).subject (or simply data.subject) returns a comma separated list. With the answer given, only the first element of the list is assigned (to allsubjects), so the effect of the above is actually:
allsubjects = data(1).subject;
The correct syntax should have been:
allsubjects = {data.subject};
to convert the list into a cell array of strings. This is what I have done in the third argument to gscatter in my answer (and what Massimo is trying to say, I assume).
To avoid dealing with comma-separated lists, conversion to cell arrays or vectors (depending on type), etc. convert your structure into a table as I've also shown.
Massimo Zanetti
Massimo Zanetti 2016 年 10 月 12 日
Sorry , I forgot the { }

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by