Plotting a Graph From a Cell Array
5 views (last 30 days)
Show older comments
I have a cell array, let's call it A, which is a 4 x 4 cell each containing a 10 x 10 numbers from 1 to 5 (the cell array attached):
A = cell(4,4);
Each cell array is like:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/877080/image.jpeg)
I want to plot a graph for each cell (for just one) and all of the cells (for all 16 cell arrays) in separate code that shows how many of the components are 1, 2,3,4 and 5. Essentially a chart similar to (I don't think Matlab can graph such a chart, but something similar):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/877085/image.jpeg)
"A" here is just one of the frame indices (I have to plot multiple cell arrays). Thank you!
0 Comments
Accepted Answer
Voss
on 28 Jan 2022
Edited: Voss
on 28 Jan 2022
First, check out the data:
load('A.mat')
whos
Kappa_x_num{1,1}
Then, do some counting:
[m,n] = size(Kappa_x_num);
max_value = 5;
counts = zeros(m,n,max_value);
for ii = 1:m
for jj = 1:n
for kk = 1:max_value
counts(ii,jj,kk) = nnz(strcmp(Kappa_x_num{ii,jj},num2str(kk)));
end
end
end
disp(counts);
Then, make a stacked bar graph (rearranging the counts to do so):
counts = reshape(counts,[m*n max_value]);
disp(counts);
figure()
bar(counts,'stacked');
ax = gca();
set(get(ax,'XAxis'), ...
'TickValues',1:m*n, ...
'TickLabel',arrayfun(@(x)sprintf('%d',x),1:m*n,'UniformOutput',false), ...
'TickLabelRotation',0);
set(get(ax,'XLabel'),'String','frame indices');
set(get(ax,'YLabel'),'String','% of points');
legend( ...
arrayfun(@(x)sprintf('''%d''',x),1:max_value,'UniformOutput',false), ...
'Location','EastOutside');
7 Comments
Voss
on 29 Jan 2022
Here's one way you can do it. Some of those files contain stuff that's not '1', '2', '3', '4', '5', so the count total is not 6400.
A = load('1249.mat');
B = load('1299.mat');
C = load('1349.mat');
D = load('1399.mat');
Kappa_x_num = cat(3,A.Kappa_x_num,B.Kappa_x_num,C.Kappa_x_num,D.Kappa_x_num);
[m,n,p] = size(Kappa_x_num);
max_value = 5;
counts = zeros(1,max_value);
for ii = 1:m
for jj = 1:n
for nn = 1:p
for kk = 1:max_value
counts(kk) = counts(kk) + nnz(strcmp(Kappa_x_num{ii,jj,nn},num2str(kk)));
end
end
end
end
disp(counts);
figure()
bar([counts; NaN(1,max_value)],'stacked');
ax = gca();
set(get(ax,'YLabel'),'String','# of points');
set(ax,'XLim',[0.2 1.8]);
legend( ...
arrayfun(@(x)sprintf('''%d''',x),1:max_value,'UniformOutput',false), ...
'Location','EastOutside');
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D 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!