Matlab: 'hold on' doesn't work with &#

2019-06-08 05:22发布

I am plotting multiple plots on the same axes by using plot() and hold(Ax, 'on') function. However, I have noticed that if I use set(Ax, 'XData', ..., 'YData', ...) for plotting instead of plot() then the hold(Ax, 'on') functionality does not work. In other words I am not able to plot all the curves together on same axes if I use set() function. Any idea why is that, or if there is a way by which I can use set() and yet use the hold on functionality? Thanks!

标签: matlab plot set
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-08 06:08

Based on this answer, you can try something like the following example:

x1 = linspace(1,10,21);
y1 = rand(1,21);

x2 = x1;
y2 = rand(1,21);

x3 = x2;
y3 = rand(1,21);

h(1) = plot(x1,y1,'b');

h(2) = copyobj(h(1),gca); 
h(3) = copyobj(h(1),gca); 

set(h(2),'XData',x2,'YData',y2,'Color','r')
set(h(3),'XData',x3,'YData',y3,'Color','g')

The set command updates the properties of the object referenced by the handle. If you don't want to modify your one curve over and over, but add curves, you have to copy the initial line object, using copyobj. Once you do that, you can generate an infinite amount of additional lines for which you can then set the properties (including new 'XData', 'YData' as you please.
However, you will need an initial line object to copy and modify.
Also, depending on how you set up your plot, consider replacing gca by your axis handle so that the reference is correct.

查看更多
登录 后发表回答