MATLAB Answers

boxplot from a structured array

22 views (last 30 days)
cgenes
cgenes on 13 Jul 2017
Commented: cgenes on 14 Jul 2017
hi
i have a structured array it's like this
name_of_image(1).stats
name_of_image(2).stats
etc
in .stats is a whole load of variables, like stats.area
what is the simplest way of producing a box plot of e.g stats.area, while ensuring that the box plot is labeled correctly with the name_of_image
thanks
ps i tried a for loop to extract the information, but thought there must be a simpler way
x = [];
% nameArray = [] ;
for i = 1 : length(collated_data)
% nameArray(i) = collated_data(i).name;
for k = 1 : length(collated_data(i).stats)
x(i, k) = collated_data(i).stats(k).area;
if x(i, k) < 17
x(i, k) = NaN;
end
end
end
  3 Comments
the cyclist
the cyclist on 13 Jul 2017
FYI, in your code the variable is named "area", but in the file it is "Area". So, you might need to fix that up in my code.

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 13 Jul 2017
Edited: the cyclist on 14 Jul 2017
numberArrays = numel(collated_data);
areaAndGroup = [];
for na = 1:numberArrays
    numberAreas = numel(collated_data(na).stats);
     % This is the tricky part. I'm extracting the area data from the inner structure, munging them into a vector, and then including a second
     % column that enumerates which structure they came from (to use as a grouping variable in the boxplot command).
    areaAndGroup = [areaAndGroup; [collated_data(na).stats.Area]',repmat(na,[numberAreas,1])];
end
areaAndGroup(areaAndGroup(:,1)<17,:) = NaN;
figure
boxplot(areaAndGroup(:,1),areaAndGroup(:,2))
  3 Comments
cgenes
cgenes on 14 Jul 2017
thanks very much

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!