3

I am plotting data in MATLAB in real time. I want to use a handle. My problem is I do not know how to plot more than one Y-Data Curve.

I found the following code It shows how to plot one set of YData. Has anybody got an idea to transform the code into two or more Y-Datasets, e.g. sind(x) as an additional curve in the plot?

x = 1:1000;
y = cosd(x);

xi = x(1);
yi = y(1);
h = plot(xi, yi, 'YDataSource', 'yi', 'XDataSource', 'xi');

for k = 2:1000...
xi = x(1:k);
yi = y(1:k);
refreshdata(h, 'caller');
drawnow;
end;
Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
user1677716
  • 113
  • 1
  • 1
  • 11

4 Answers4

3

First of all, never use refreshdata. Use the direct set method instead.

        set(h,'Xdata',xi,'YData',yi);

Secondly, you should do two plots

      h1 = plot(xi, yi);
      h2 = plot(xi, yi);

And update each one accordingly.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
  • Thanks very much so far Andrey. If I do two plots, doesn' t that mean there will be two figures? Am a Matlab Beginner, so pardon me if this idea is stupid. – user1677716 Oct 25 '12 at 13:27
  • Andrey, why should you not use `refreshdata`? – angainor Oct 25 '12 at 13:58
  • @angainor, a very good question. I just wrote it as question and answer http://stackoverflow.com/questions/13102654/how-should-i-update-the-data-of-a-plot-in-matlab. – Andrey Rubshtein Oct 27 '12 at 17:54
3

The below code works for me, if you really want to use handles

x = 1:1000;
y = cosd(x);
y2 = sind(x);

xi = x(1);
yi = y(1);
yi2 = y2(1);
figure(1); clf;
h = plot(xi, yi, 'YDataSource', 'yi', 'XDataSource', 'xi');
hold on;
h2 = plot(xi, yi2, 'YDataSource', 'yi2', 'XDataSource', 'xi');

for k = 200:1000
    xi = x(1:k);
    yi = y(1:k);
    yi2 = y2(1:k);
    refreshdata(h);
    refreshdata(h2);
    drawnow;
end;

You do need a hold on.

Also, instead of refreshdata you can use set as Andrey suggested:

set(h,'Xdata',xi,'YData',yi);
set(h2,'Xdata',xi,'YData',yi2);
angainor
  • 11,760
  • 2
  • 36
  • 56
  • Thanks very much! For me your example did it! The real time plot works now showing multiple curves. Great Answer! – user1677716 Oct 25 '12 at 16:16
1

Are you maybe looking for the hold command?

plot(1 : 10, (1 : 10).^2, 'r')
hold on
plot(1 : 10, (1 : 10).^3)

EDIT:

You can use hold in combination with set to update the plot (see also Andrey's answer):

h1 = plot(1 : 10, (1 : 10).^2, 'r');
hold on;
h2 = plot(1 : 10, (1 : 10).^3);
set(h1, 'XData', 1 : 2 : 20);
set(h2, 'YData', 0.1 * (1 : 20).^3);

The axes will update automatically.

Community
  • 1
  • 1
Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
  • Hey Florian. I read that when doing real time plots it is supposed to be more efficient to use handles. Apart from that, hold on freezes my axes (I believe at last so) which I do not desire. The implementation works well with handles apart from the fact that I am not able to plot multiple lines. – user1677716 Oct 25 '12 at 13:35
  • @user1677716 `hold on` does not freeze your axes. – angainor Oct 25 '12 at 13:44
  • @user1677716: You can use `hold` together with handles, see my edit. Also, as angainor pointed out, the axes will still auto-update. – Florian Brucker Oct 25 '12 at 13:51
  • 3
    If you like hold, "hold on" will give you multiple curves in the same color. Use "hold all" to get different colored curves (as if you had plotted all the curves with one plot command). – engineerC Oct 25 '12 at 14:03
0

If you don't care too much about displaying the same colour for all curves, simply concatenate the x data into a single vector separated by NaN between the curve components (do a similar thing for the y data). Then the "plot" command can take in these larger x and y vectors and will display everything at once. You may get around the colour issue by doing something similar with the colordata array.