How to draw a bar graph from cell array with different size length?
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
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.
采纳的回答
Matt Tearle
2012-1-16
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 个评论
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.
更多回答(2 个)
C.J. Harris
2012-1-16
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')
Walter Roberson
2012-1-16
L = max(cellfun(@length, a));
bar( cell2mat( @(V) [V, zeros(1,L-length(V))], a, 'Uniform', 0), 'group' )
3 个评论
Walter Roberson
2012-1-16
bar( cell2mat( cellfun(@(V) [V, zeros(1,L-length(V))], a, 'Uniform', 0)), 'group' )
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!