Createing a boxplot from callArrays

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.
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일

1 개 추천

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:

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

질문:

2023년 3월 9일

댓글:

2023년 4월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by