Clear Filters
Clear Filters

Matlab Live Scripts displaying figures in seemingly random order when multiple code blocks with subplots are used

73 views (last 30 days)
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);
ax1(1) = subplot(2,1,1);
grid on
title('s1 aligned')
axis tight
ax1(2) = subplot(2,1,2);
grid on
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);
ax2(1) = subplot(2,1,1);
grid on
title('d2s1 aligned')
axis tight
ax2(2) = subplot(2,1,2);
grid on
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.

Answers (2)

Walter Roberson
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.
cla() %figure 2 will draw at this point
hold on
%figure 1 draws because you reached the end of the script
Dylan Green
Dylan Green on 10 Feb 2021
What is confusing to me is that I do essentially the same thing all the time. There are many instances where in a code block I use plot(), the figure displays, and then the next code block does the same and everything works. It is not until this particular block which uses subplot() that things fail, and further it is only when I have more than one of them. Given that Live Scripts are Matlab's answer to Jupyter notebooks, it is frustrating that they do not work in a similarly intuitive way.
Thanks for the help, in any case.
Dylan Green
Dylan Green on 10 Feb 2021
I've realized that the line that breaks this is the call to linkaxes(). If this is remove the problem goes away, although that unfortunately makes the plots quite a bit uglier.

Sign in to comment.

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);

Community Treasure Hunt

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

Start Hunting!