How to draw a bar graph from cell array with different size length?

For example, i have a = [{1}; {[3 5]}; {[4 6 7]}; {[3 6 7 9]}]. How do I plot the cell array in one bar graph?

4 件のコメント

Walter Roberson
Walter Roberson 2012 年 1 月 16 日
What would you like the resulting graph to look like?
Chin
Chin 2012 年 1 月 16 日
A bar graph that consist of 4 group. The first group just has 1 column (1), second group consist 2 column (3 & 5), third group consist of (4, 6 & 7) and so on. You can refer to the link below.
http://www.infocaptor.com/img/charts/bar_chart.png
Thank you.
C.J. Harris
C.J. Harris 2012 年 1 月 16 日
How would you link the data you have provided to the legend (as shown in your example figure)? In the example groups 3 & 5 have only two bars, but it is clearly item 003 which is missing.
The data you have provided doesn't make it clear which elements are missing from each group - and therefore you run the risk of having very misleading bar colours.
Chin
Chin 2012 年 1 月 16 日
Actually i can plot it using excel but I would like to try it from matlab see it can be done via matlab. Is that possible to append 0 behind the cell array so that a = [{[1 0 0 0]}; {[3 5 0 0]}; {[4 6 7 0]}; {[3 6 7 9]}] and then plot it in grouped bar graph?

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

 採用された回答

Matt Tearle
Matt Tearle 2012 年 1 月 16 日

0 投票

This will extract the values and add zeros to the end, then extract all to a matrix. But are you sure the missing values are always at the end? A safer approach would be to import with NaN placeholders where the values are missing. How are you importing the data?
a = [{1}; {[3 5]}; {[4 6 7]}; {[3 6 7 9]}];
n = max(cellfun(@length,a));
f = @(x) [x,zeros(1,n-length(x))];
a = cellfun(f,a,'UniformOutput',false);
x = cat(1,a{:})
bar(x')

4 件のコメント

Chin
Chin 2012 年 1 月 16 日
Yes, the missing values is always is in the end, but the resulting graph that for this code is not the graph that i want. I would it to show in first group just contains one bar, second group contains two bar and so on but this resulting graph is vice versa.
Chin
Chin 2012 年 1 月 16 日
I know what is the problem already. I just need to change bar(x') to bar(x) then it will get the result that i want. Thank you so much.
Matt Tearle
Matt Tearle 2012 年 1 月 16 日
Oh, yes, sorry I misunderstood which way you wanted them grouped. That's good: bar(x) is even neater than having to transpose.
Chin
Chin 2012 年 1 月 16 日
sorry for another question. now i am facing another problem. if the cell array a = [[{1}; {[3 5]}; {[4 6 7]}; {[3 6 7 9]}] [{3}; {[7 1]}; {[8 2 7]}; {[3 5 7 4]}]]. How do I plot it into two bar graph? Which means that a{1, 1} will have first bar graph while a{1, 2} will have another bar graph. Thank you.

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

その他の回答 (2 件)

C.J. Harris
C.J. Harris 2012 年 1 月 16 日

0 投票

If you are going to pad out your arrays with zeros you don't even need to define it as a cell array, just use a matrix.
Try this:
a = [1 0 0 0; 3 5 0 0; 4 6 7 0; 3 6 7 9];
bar(a,'group')

1 件のコメント

Chin
Chin 2012 年 1 月 16 日
However, my result is generated in different size cell array.

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

Walter Roberson
Walter Roberson 2012 年 1 月 16 日

0 投票

L = max(cellfun(@length, a));
bar( cell2mat( @(V) [V, zeros(1,L-length(V))], a, 'Uniform', 0), 'group' )

3 件のコメント

Chin
Chin 2012 年 1 月 16 日
when i run this code it said '??? Error using ==> cell2mat
Too many input arguments.'.
Walter Roberson
Walter Roberson 2012 年 1 月 16 日
bar( cell2mat( cellfun(@(V) [V, zeros(1,L-length(V))], a, 'Uniform', 0)), 'group' )
Chin
Chin 2012 年 1 月 16 日
thanks. This is correct graph. Besides, now I am plotting another two graph from cell array a = [[{1}; {[3 5]}; {[4 6 7]}; {[3 6 7 9]}] [{3}; {[7 1]}; {[8 2 7]}; {[3 5 7 4]}]]. One is graph is from a{:,1} and the second one is from a{:, 2}. I can plot the bar graph but the bar graph consists of same colour compare to only one graph that can have multiple colour. What I am missing? Thanks again for helping.

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

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by