Createing a boxplot from callArrays

2 次查看(过去 30 天)
Hello,
I am struggling with the following problem. I have data with double value in 32 classes a Cell Array. The 32 classes have a different amount of double values. Furthermore, I have the labels for these classes in another CellArray. I want to create a graphic that shows the boxplot of all classes with the correct labels on the x Axis. I found one solution that uses boxplotgroup from file exchange, but this solution didn't work for me. Does anyone have an idea?
  2 个评论
Rik
Rik 2023-3-9
You should either attach your data or create some example data, and you should show what code you tried, preferably within the editor. If you have a lot of code, you can attach them in an m-file.
Do I understand you correctly that you have a cell array, where each cell contains an array (of type double), and that each cell should become a single box on your boxplot?
You failed to mention your release and whether you have the stats toolbox, so I don't know whether you can actually use the boxplot function, or have access to the boxchart function.
Martin Krüger
Martin Krüger 2023-3-9
Thanks a lot for your answer.
I do have the stats toolbox and I am working with MATLAB R2022b
The data looks similar to:
c = arrayfun(@(i){rand(randi([5,20]),1)},1:32)
And the labels do look like this
years = compose('%d',1901:1932)

请先登录,再进行评论。

回答(1 个)

Arka
Arka 2023-3-9
Hi,
I think this is what you are looking for:
c = arrayfun(@(i){rand(randi([5,20]),1)},1:32);
maxLen = max(cellfun('size', c, 1)); % find max length of datas for all categories, i.e. which category contains the largest amount of data
for i = 1:size(c,2)
c{i} = padarray(c{i}, maxLen-length(c{i}), NaN, 'pre'); % pad the other categories with NaNs
end
c = cell2mat(c); % now we can convert the cell array to matrix, since all rows are of same length
years = compose('%d',1901:1932);
boxplot(c, years);
Since the number of datas in each column was different, you just needed to make them equal so that they can be converted to a matrix.
If you wish to learn more about padarray, please check out the MathWorks documentation page about the same:

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by