Set Ydata in Matlab, how does it work?

Hello, i came across this code.
plot_handle=plot(zeros(num_samples,2));
then in another function which is called update the plot.
i saw this.
data = getdata(daq_object,num_samples);
for i = 1:length(plot_handle)
set(plot_handle(i),'YData',data(:,i));
end
I don't get how the set(plot_handle(i),'YData',data(:,i)); works, beacuse in the plot_handle function, i only have num_samples as input.

 채택된 답변

Oleg Komarov
Oleg Komarov 2011년 7월 22일

1 개 추천

I think it's a real time update (since it ivolves a daq object) of your plot.
A general example is:
h = plot(rand(10,1))
for n = 1:20
set(h,'Ydata',rand(10,1))
pause(.1)
end

댓글 수: 3

rave
rave 2011년 7월 22일
i tired to expand this to, more variables, since i will be doing a spectrogram function and surf.
i tried:
h=plot(rand(10,3),rand(10,1));
for n=1:20
set(h,'Ydata',rand(10,1),rand(10,4));
end
but getting errors
Oleg Komarov
Oleg Komarov 2011년 7월 23일
Your 3 lines have the same Ydata but only the Xdata changes.
So it's again set(h,'Ydata',rand(10,1)); in the loop.
Oleg Komarov
Oleg Komarov 2011년 7월 23일
Or if you wanna give it 3 different Ydatas:
set(h,{'Ydata'},num2cell(rand(3,10),2));

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 7월 22일

1 개 추천

Plot a figure:
h=plot(1:10);
type get(h) to see a list of properties. You can get the data back by:
y=get(h,'YData');
You can also change the figure by:
set(h,'YData',rand(1,10);

댓글 수: 2

rave
rave 2011년 7월 22일
i tired to expand this to, more variables, since i will be doing a spectrogram function and surf.
i tried:
h=plot(rand(10,3),rand(10,1));
for n=1:20
set(h,'Ydata',rand(10,1),rand(10,4));
end
but getting errors
Fangjun Jiang
Fangjun Jiang 2011년 7월 22일
In your code, h=plot(rand(10,3),rand(10,1)) returns 3 handles indicating 3 curves. When you try to set(h), you need to provide proper data. Check out the other property such 'XData','ZData', 'CData'(for image). You'll get the idea.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 7월 23일

1 개 추천

When the Y value is not a vector, plot() will produce one curve for each column of Y. Your initial Y has two columns (unless numsamples is only 1), so plot() will produce two lineseries objects.
If you have more than one channel in your daq_object, then if you request to read numsamples samples, you are requesting to read that many per channel.
The rest of the code is then appropriate to update the respective plot lines according to the data received for each channel.

댓글 수: 1

rave
rave 2011년 7월 26일
what do you mean by "you are requesting to read that many per channel", i am hoping to read in 2 channels in the daq_object.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

태그

질문:

2011년 7월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by