Is it possible to plot the output plot of multiple function on the same figure?

5 views (last 30 days)
This is what the function looks like and I have multiple sets of inputs. I was wondering if there was anyway to have the output plots of multiple functions on the same figure? If not, I will have to edit the function to have more inputs and then plot them inside the function.
function obj(Ycord,Xcord,Yvel,Xvel)
for k=1:length(Ycord)
clf
for i=1:5
hold on
grid on
plot(-Xcord(k,i),Ycord(k,i),'.');
quiver(-Xcord(k,i),Ycord(k,i),-Xvel(k,i),Yvel(k,i),'LineWidth',1);
rectangle('Position',[-1 -1 2 4],'EdgeColor', 'm','LineWidth',2);
rectangle('Position',[-Xcord(k,i)-1,Ycord(k,i),2,4],'EdgeColor','k','LineWidth', 1);
end
xlim([-40, 40]);
ylim([-40, 40]);
pause(0.1)
end
end

Answers (1)

Walter Roberson
Walter Roberson on 14 Jun 2022
generally speaking you can use "hold on" or use subplot() or tiled layouts. However none of those will work for you as long as you keep that clf that is erasing everything in the figure
  2 Comments
Yash
Yash on 14 Jun 2022
since the plot is an animation, the clf is being used to clear the previous frame. Hold on works when there are multiple plots within the same function, but I was wondering if it was possible to "hold on" to the output plots of multiple function calls and put them onto the same figure.
Walter Roberson
Walter Roberson on 14 Jun 2022
MATLAB has "high level" and "low level" graphics calls.
If you call the right functions with the right parameters, then MATLAB does not check the graphics state and just adds the new graphics to whatever is there.
But most graphics calls are considered to be "high level" calls. They check the NextPlot property of the axes, and if it is set to Replace, then the graphics call clears the axes before drawing. "hold on" sets the NextPlot property to Add instead of Replace.
Notice that nothing in this is checking to see whether the graphics calls were made inside the same function. The system only cares about the current setting of NextPlot.
It is therefore completely valid to set hold on, and then make any number of function calls that eventually add graphics. As long as those functions do not clear the figure or clear the axes or turn off the hold or deliberately delete graphics, then the graphics will just keep building up, no matter which function makes the graphics call.
But.. clf removes all current graphics in the figure.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!