Plotting a Graph From a Cell Array
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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:

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):

"A" here is just one of the frame indices (I have to plot multiple cell arrays). Thank you!
採用された回答
First, check out the data:
load('A.mat')
whos
Name Size Bytes Class Attributes
Kappa_x_num 4x4 171264 cell
ans 1x30 60 char
Kappa_x_num{1,1}
ans = 10×10 cell array
{'4'} {'4'} {'1'} {'3'} {'1'} {'1'} {'1'} {'1'} {'5'} {'5'}
{'5'} {'1'} {'1'} {'3'} {'1'} {'1'} {'1'} {'5'} {'1'} {'1'}
{'1'} {'1'} {'5'} {'3'} {'3'} {'1'} {'1'} {'1'} {'5'} {'1'}
{'5'} {'5'} {'1'} {'1'} {'1'} {'1'} {'1'} {'1'} {'1'} {'5'}
{'4'} {'5'} {'1'} {'4'} {'1'} {'1'} {'1'} {'1'} {'5'} {'1'}
{'5'} {'1'} {'5'} {'3'} {'1'} {'1'} {'3'} {'1'} {'5'} {'1'}
{'1'} {'1'} {'3'} {'3'} {'1'} {'1'} {'1'} {'5'} {'5'} {'5'}
{'4'} {'1'} {'5'} {'3'} {'1'} {'1'} {'1'} {'1'} {'1'} {'5'}
{'1'} {'1'} {'1'} {'1'} {'1'} {'1'} {'1'} {'1'} {'5'} {'5'}
{'5'} {'1'} {'5'} {'1'} {'1'} {'1'} {'3'} {'1'} {'5'} {'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);
(:,:,1) =
61 74 71 71
100 71 71 100
100 6 100 100
100 100 100 100
(:,:,2) =
0 0 0 0
0 0 0 0
0 37 0 0
0 0 0 0
(:,:,3) =
10 1 3 2
0 1 1 0
0 0 0 0
0 0 0 0
(:,:,4) =
5 0 0 2
0 0 0 0
0 56 0 0
0 0 0 0
(:,:,5) =
24 25 26 25
0 28 28 0
0 1 0 0
0 0 0 0
Then, make a stacked bar graph (rearranging the counts to do so):
counts = reshape(counts,[m*n max_value]);
disp(counts);
61 0 10 5 24
100 0 0 0 0
100 0 0 0 0
100 0 0 0 0
74 0 1 0 25
71 0 1 0 28
6 37 0 56 1
100 0 0 0 0
71 0 3 0 26
71 0 1 0 28
100 0 0 0 0
100 0 0 0 0
71 0 2 2 25
100 0 0 0 0
100 0 0 0 0
100 0 0 0 0
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 件のコメント
MarshallSc
2022 年 1 月 28 日
Thanks a lot mate!
MarshallSc
2022 年 1 月 29 日
Sir, how can I compile all of the charts (16) into one bar, so that instead of 16 bars I'd have only one? Basically to see how many points correspond to a value for the 16 cell arrays in one bar. Thank you in advance.
Here's one way. I had to add a second row (of all NaNs) to the input to bar() to get it to do the right thing, and then set the axes XLim afterward. I don't know if there's a better way.
load('A.mat')
[m,n] = size(Kappa_x_num);
max_value = 5;
counts = zeros(1,max_value);
for ii = 1:m
for jj = 1:n
for kk = 1:max_value
counts(kk) = counts(kk) + nnz(strcmp(Kappa_x_num{ii,jj},num2str(kk)));
end
end
end
disp(counts);
1325 37 18 63 157
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');

MarshallSc
2022 年 1 月 29 日
Thank you again so much!
MarshallSc
2022 年 1 月 29 日
I'm really sorry to ask you another question, the last one; but if I have multiple data files (A,B,C,...), how can I plot them in one graph? I tried to do that myself but I can't get them all in one graph. For example, I've attached 4 data files (1249,1299, 1349, 1399), could you please help me with this final question ,I'd really appreciate it. Sorry again!
Voss
2022 年 1 月 29 日
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);
4931 99 655 103 157
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');

MarshallSc
2022 年 1 月 29 日
Thanks a lot Ben, really appreciate you!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で 2-D and 3-D Plots についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
