如何在MATLAB中使用句柄绘制多条曲线?

时间:2021-11-28 23:49:06

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.

我正在用MATLAB实时绘制数据。我想用把手。我的问题是我不知道如何绘制一个以上的y数据曲线。

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?

我找到了下面的代码,它展示了如何绘制一组YData。有没有人想过把代码转换成两个或更多的y数据集,例如sind(x)作为图中的附加曲线?

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;

4 个解决方案

#1


2  

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代替refreshdata,正如Andrey建议的:

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

#2


3  

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

首先,不要使用refreshdata。使用直接集方法代替。

        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.

并相应更新。

#3


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):

你可以使用hold结合set更新剧情(参见Andrey的答案):

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.

坐标轴将自动更新。

#4


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.

如果您不太关心为所有的曲线显示相同的颜色,只需将x数据连接到一个由NaN在曲线组件之间分隔的单个向量(对y数据执行类似的操作)。然后“plot”命令可以接收这些较大的x和y向量,并立即显示所有内容。您可以通过使用与colordata数组类似的方法来解决颜色问题。

#1


2  

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代替refreshdata,正如Andrey建议的:

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

#2


3  

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

首先,不要使用refreshdata。使用直接集方法代替。

        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.

并相应更新。

#3


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):

你可以使用hold结合set更新剧情(参见Andrey的答案):

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.

坐标轴将自动更新。

#4


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.

如果您不太关心为所有的曲线显示相同的颜色,只需将x数据连接到一个由NaN在曲线组件之间分隔的单个向量(对y数据执行类似的操作)。然后“plot”命令可以接收这些较大的x和y向量,并立即显示所有内容。您可以通过使用与colordata数组类似的方法来解决颜色问题。