Matlab Live Scripts displaying figures in seemingly random order when multiple code blocks with subplots are used
Show older comments
I am working in a Live Script where I am trying to align signals within different data sets and plot the results for each set. This is in a long script with many previous figures. The signal alignment/plotting code is as follows:
% for dataset 1
d1s1_aligned = alignsignals(d1s1, d1s2);
figure
ax1(1) = subplot(2,1,1);
plot(d1s1_aligned)
grid on
title('s1 aligned')
axis tight
ax1(2) = subplot(2,1,2);
plot(d1s2)
grid on
title('s2')
axis tight
linkaxes(ax1, 'xy')
This is for the first data set (as denoted by d1). If I only try to do this for one data set everything works fine, but I want to repeat this code block multiple times for d2, d3, etc. If, for instance, I add this block underneath:
% for dataset 2
d2s1_aligned = alignsignals(d2s1, d2s2);
figure
ax2(1) = subplot(2,1,1);
plot(d2s1_aligned)
grid on
title('d2s1 aligned')
axis tight
ax2(2) = subplot(2,1,2);
plot(d2s2)
grid on
title('d2s2')
axis tight
linkaxes(ax2, 'xy')
(the same code but d1 -> d2, ax1 -> ax2) all figures in the document end up being rendered at the bottom, out of order, and the two code blocks get merged. It seems maybe live scripts fail when there are multiple subplots in a script? Any advice would be much appreciated.
1 Comment
Emilie Herpain
on 23 Oct 2022
For me, what worked is to add the command 'close all' before a new figure that I wanted to generate.
Answers (2)
Walter Roberson
on 10 Feb 2021
Because Live Script renders changes in-line, it postpones rendering until one of the following occurs:
- the axes is cleared or deleted (in which case the content immediately prior to deletion is displayed); or
- there is a return to the command line (such as the end of the Live Script -- but also because of debugging.)
Notice that I did not say "or figure() is called". Regular (non-Live) scripts update graphics when drawnow() or pause() or uiwait() or waitfor() or figure() are called, or the command line is returned to, but the same rules are not followed for Live Script. That is because you might have later statements in your code that switch back to the original figure and draw more in the figure.
figure(1)
plot(rand(1,20))
figure(2)
plot(randn(1,20))
cla() %figure 2 will draw at this point
figure(1)
hold on
plot(rand(1,20).^2);
%figure 1 draws because you reached the end of the script
4 Comments
Dylan Green
on 10 Feb 2021
Hmmm... I don't know anymore ;-)
In Live Script, the below code renders figure 2 first if and only if the code does not randomly put the text label in figure 2. But at the place it renders figure 2, it cannot know whether or not there is going to be later activity. So we have to conclude that if objects are not cleared or deleted, that the rendering is inserted at the place where there was the last activity for that figure, and that in turn implies that in such cases the figures are not drawn until the end of the script.
The question would then become what we can do to trigger earlier release deliberately, or what we need to do in order to avoid triggering earlier release, and I am no longer sure :(
figure(1)
plot(rand(1,20), 'b')
title('1')
figure(2)
plot(randn(1,20), 'r')
title('2')
hold on %figure 2
figure(1)
hold on %figure 1
plot(randn(1,20), 'g');
hold off
figure(2)
if rand() < 0.5
text(5,.5, 'randomly in 2')
end
Dylan Green
on 10 Feb 2021
Dylan Green
on 10 Feb 2021
Diaa
on 28 Dec 2021
As a workaround, you can use this MATLAB mechanism in your favor by creating a duplicate dummy figure then delete it to show the intended contents at your specified code location
dummyFig = figure;
copyobj(<handle of some axes in the intended parent figure >, dummyFig);
clf(dummyFig)
1 Comment
Duncan Carlsmith
on 26 Jul 2024
This doesn't work for me.
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
