Createing a boxplot from callArrays

2 views (last 30 days)
Martin Krüger
Martin Krüger on 9 Mar 2023
Commented: Martin Krüger on 1 Apr 2023
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 Comments
Rik
Rik on 9 Mar 2023
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 on 9 Mar 2023
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)

Sign in to comment.

Answers (1)

Arka
Arka on 9 Mar 2023
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:

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!