Createing a boxplot from callArrays

1 回表示 (過去 30 日間)
Martin Krüger
Martin Krüger 2023 年 3 月 9 日
コメント済み: Martin Krüger 2023 年 4 月 1 日
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:
  1 件のコメント
Martin Krüger
Martin Krüger 2023 年 4 月 1 日
Thanks a lot!
This is really helpful.

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

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by