Search code examples
matlabplotmatlab-figure

Live Script with animation


MATLAB 2016a introduced Live Scripts, allowing to show plotting output next to the script. Is it somehow possible to show animations? For example, the following code in a regular script will plot a few points and then rotate the axes:

x = rand(10, 3);
plot3(x(:, 1), x(:, 2), x(:, 3), 'o')
for ii = 1:360
    camorbit(1, 10*cos(ii/90*pi)*pi/45)
    drawnow
    pause(0.01)
end

If this is embedded in a Live Script, the initial plot is shown, then seemingly nothing happens while the loop is running, then the last aspect (which is the same as the original plot) is shown in a new display item.

Alternatively, is there an option to interact with the plots in a live script (other than double-clicking to open the plot in a new figure)? E.g. rotate3d does not have an effect.

Edit: As of release 2019a, animations are possible as per release notes.


Solution

  • The example code as posted in the question produces a rotating plot as of MATLAB 2019a. It does not work yet in 2018b. The release notes for 2019a mention that

    You can enable for-loop animations in the Live Editor to show changes in plotted data over time. To enable animations in the Live Editor, set the matlab.editor.AllowFigureAnimations setting to true:

    s = settings;
    s.matlab.editor.AllowFigureAnimation.PersonalValue = true;
    

    Running these two lines before the example script will yield the expected behaviour.