Faster alternative to subplot

10 Ansichten (letzte 30 Tage)
Samuel Lehmann
Samuel Lehmann am 9 Mär. 2016
Kommentiert: Jan am 28 Feb. 2023
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 Kommentare
Bruno Luong
Bruno Luong am 28 Feb. 2023
Bearbeitet: Bruno Luong am 28 Feb. 2023
@ED If you update tye plot, take a look at animatedline
Jan
Jan am 28 Feb. 2023
@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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Walter Roberson
Walter Roberson am 9 Mär. 2016
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 am 9 Mär. 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

Community Treasure Hunt

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

Start Hunting!

Translated by