Bargraph from variable length structure

I have a structure compiled from imported (x,y) datasets in the form of variable.dataset(i).x(:), variable.dataset(i).y(:) where the lengths of x&y are the same within the same dataset, but may differ between datasets i.e.:
variable.dataset(1).x = [1 2 3 4]';
variable.dataset(1).y = [9 8 7 6]';
variable.dataset(2).x = [3 4 5 6 7 8]';
variable.dataset(2).y = [1 9 2 8 3 7]';
is there a way to generate a single bar(x,y) figure containing all the x/y dataset pairs?

 採用された回答

Matt Fig
Matt Fig 2012 年 9 月 25 日
編集済み: Matt Fig 2012 年 9 月 25 日

0 投票

As long as your x data does not contain duplicates:
% Data. Note the x has no duplicates.
variable.dataset(1).x = [1 2 3 4]';
variable.dataset(1).y = [9 8 7 6]';
variable.dataset(2).x = [5 6 7 8 9 10]';
variable.dataset(2).y = [1 9 2 8 3 7]';
bar(vertcat(variable.dataset(:).x),vertcat(variable.dataset(:).y))
If you meant that you want two bar plots on the same figure, do this:
bar(variable.dataset(1).x,variable.dataset(1).y)
hold on
bar(variable.dataset(2).x,variable.dataset(2).y,'r')

8 件のコメント

Adam
Adam 2012 年 9 月 25 日
編集済み: Adam 2012 年 9 月 25 日
in your example you modified dataset(2)'s range such that they don't overlap. if you use the original dataset, you get the error:
Error using bar (line 60)
XData cannot contain duplicate values.
Additionally, this method no longer maintains the integrity of dataset(1) compared to dataset(2).
Matt Fig
Matt Fig 2012 年 9 月 25 日
編集済み: Matt Fig 2012 年 9 月 25 日
Yes, as I said,
"As long as your x data does not contain duplicates."
Does the second example work for you? The second example I posted will work just fine with your original data. If you need it to all be one color instead of red and blue, just leave out the 'r' part.
Adam
Adam 2012 年 9 月 25 日
In your later code, it plots the data on top of each other. This could allow for data to be masked by other datasets.
What i am looking to accomplish would need to be done with one bar() function call in order for the data to be place side by side instead of using hold which places them on top of each other.
any ideas?
Matt Fig
Matt Fig 2012 年 9 月 25 日
But let's think about this for a second. If in one data set you have y = 7 at x = 3 and at another you have y = 1 at x = 3, how could you possibly have only one bar represent both those things? It seems you either need to choose one set of data (my first example) or overlap them (my second example).
What does your third alternative actually look like? In other words, what do we see when we look for the height of the bar at x = 3?
Adam
Adam 2012 年 9 月 25 日
編集済み: Adam 2012 年 9 月 25 日
I am not looking for one bar to represent both. Where there is overlap, i am looking for 2 bars next to each other; using your example: at x=3, one bar is length 1 and the other is length 7 both next to each other.
For locations where there isn't any overlap, i would expect the same place holders but a height of 0 for the non overlapping dataset.
Matt Fig
Matt Fig 2012 年 9 月 25 日
編集済み: Matt Fig 2012 年 9 月 25 日
Aha, now that I can work with! Here is one method:
variable.dataset(1).x = [1 2 3 4]';
variable.dataset(1).y = [9 8 7 6]';
variable.dataset(2).x = [3 4 5 6 7 8]';
variable.dataset(2).y = [1 9 2 8 3 7]';
MAT = nan(length(unique(vertcat(variable.dataset(:).x))),2);
MAT(variable.dataset(1).x,1) = variable.dataset(1).y;
MAT(variable.dataset(2).x,2) = variable.dataset(2).y;
bar(MAT)
If your real data has, say 4 instead of 2 sets, the replace the 2 in the first command with 4, and the two middle commands could be put into a loop.
for ii = 1:length(variable.dataset)
MAT(variable.dataset(ii).x,ii) = variable.dataset(ii).y;
end
Adam
Adam 2012 年 9 月 25 日
I was using a somewhat trivial case for the example datasets - I don't believe this works if x is a noninteger i.e.
variable.dataset(1).x = (1:0.25:100)';
any ideas?
Matt Fig
Matt Fig 2012 年 9 月 25 日
編集済み: Matt Fig 2012 年 9 月 25 日
Hopefully you have left nothing out this time!
variable.dataset(1).x = [1 2 3 4]';
variable.dataset(1).y = [9 8 7 6]';
variable.dataset(2).x = [3 4 5 6 7 8]';
variable.dataset(2).y = [1 9 2 8 3 7]';
variable.dataset(3).x = [1.5 2.5 3.5].'; % Non-integers.
variable.dataset(3).y = [6 7 8].';
variable.dataset(4).x = [1.5 2.5 3.5 6.5].';
variable.dataset(4).y = [2 1 3 8].';
U = unique(vertcat(variable.dataset(:).x));
MY = nan(length(U),length(variable.dataset));
MX = repmat(U,1,size(MY,2));
for ii = 1:length(variable.dataset)
R = ismember(MX(:,1),variable.dataset(ii).x);
MY(R,ii) = variable.dataset(ii).y;
end
bar(MX,MY)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

製品

質問済み:

2012 年 9 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by