Bargraph from variable length structure

조회 수: 3 (최근 30일)
Adam
Adam 2012년 9월 25일
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일
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일
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개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by