boxplot from a structured array

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 个评论

Can you upload a MAT file with this variable in it?
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.

请先登录,再进行评论。

 采纳的回答

the cyclist
the cyclist 2017-7-13
编辑:the cyclist 2017-7-14
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 个评论

yes thanks very much for that very nice. But i actually want the names of the images, not the numbers on the x-axis.
I think i'm storing my data wrong - - I should not use a structured array , but instead have a character array for image names, and arrays for variables such as area -- much like the box plot example load carsmall.
if I had image X, and wanted to stored variable (from regionprops) Y and Z, how would I go about storing the data like in the example 'load carsmall'?
PS if this is worthy of another topic I will accept your first answer!! :-)

Well, you've got some awfully long names there. The main problem will be parsing those into something that will fit. Here is a not-very-robust attempt:

numberArrays = numel(collated_data);
areaAndGroup = [];
for na = 1:numberArrays
    numberAreas = numel(collated_data(na).stats);
    shortName{na} = collated_data(na).name(41:end-14);
     % This is the tricky part. I'm extracting individual area, munging them into a vector, and then including a second
     % column enumerated which structure they came from (to use as a grouping variable).
    areaAndGroup = [areaAndGroup; [collated_data(na).stats.Area]',repmat(na,[numberAreas,1])];
end
areaAndGroup(areaAndGroup(:,1)<17,:) = NaN;
figure
boxplot(areaAndGroup(:,1),areaAndGroup(:,2))
set(gca,'XTickLabel',shortName,'XTickLabelRotation',30)

You could do something smarter using regular expressions to identify the relevant part of the longer name.

thanks very much

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by