Faster alternative to subplot

Hello, I have a program with four different plots that I am updating multiple times each second. In order to select the plot before I modify it, I am using the subplot function, but unfortunately this function is slowing down my program significantly. As a result, I was wondering if there was an alternative, faster version of subplot if you don't actually need to create a subplot, but simply select a preexisting subplot in order to write to it.
Thank you for any help that you can provide.

3 Comments

ED
ED on 28 Feb 2023
Moved: Bruno Luong on 28 Feb 2023
hi,
although logical, this is not necessarily true.
i have tried it and timed it with tic-toc and subplot was faster than axes.
In my case the problem is that I keep adding objects to the axes, so, even though they start in a similar wasy, as time goes by, subplot remains the same and axes gets slower.
hope M gurus find the right answer.
Bruno Luong
Bruno Luong on 28 Feb 2023
Edited: Bruno Luong on 28 Feb 2023
@ED If you update tye plot, take a look at animatedline
@ED: Looking at the code of subplot.m it is surprising, that a direct call to axes should be slower. The overhead of subplot is large and finally axes is called here also.
Instead of making a specific axes object the current one, it is faster to use it as parent for newly created objects. It is even faster to update existing objects than to create new ones. See Bruno's suggestion, which does exactly this.
Repeated calls of axes get slower, if new axes objects are created instead of activating existing axes. Maybe it gets clear, what you observe, if you post a minimal working example.

Sign in to comment.

Answers (2)

Record the handles created by subplot(), and then axes() the proper one.
ax11 = subplot(2,2,1);
ax12 = subplot(2,2,2);
ax21 = subplot(2,2,3);
ax22 = subplot(2,2,4);
for K = 1 : 10
axes(ax11)
....
axes(ax12)
...
etc
end
Probably you can do even better than this by recording the handles of the graphics objects generated the first time, and after that updating their properties.
for K = 1 : 10
y11 = rand(1,10);
y12 = rand(1,10);
if K == 1
%first iteration, create the graphics
h11 = plot(ax11, t, y11);
title(ax11, 'first plot');
h12 = plot(ax12, t, y12);
title(ax12, 'second plot);
else
%for everything other than the first, just update the existing graphs
set(h11, 'ydata', y11);
set(h12, 'ydata', y12);
end
end
Steven Lord
Steven Lord on 9 Mar 2016
Don't keep recreating the axes or forcing SUBPLOT to check if it should return the handle of an existing axes. Store the handles of the axes in a variable and use AXES to make the appropriate axes current (or explicitly pass in the handle to the appropriate axes into the plotting function; most accept axes as inputs in this way.)

Tags

Asked:

on 9 Mar 2016

Commented:

Jan
on 28 Feb 2023

Community Treasure Hunt

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

Start Hunting!