Live Script with animation
Asked Answered
F

3

14

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.

Fauver answered 30/8, 2016 at 15:5 Comment(6)
I don't have matlab 2016 to try this, but you could try a few things: 1) in the old publisher you had to use snapnow instead of drawnow if you wanted a snapshot in the publisher. If Live Scripts have borrowed code from publisher then this might be worth a try. 2) If instead of a for-loop you create an actual movie object and play it back with the movie command, the LiveScript might be set up to interpret it correctly. 3) If the LiveScript is proper html-based then it may support animated gifs; create one using imwrite and individual frames. (Let me know if any of the above work. :) )Chimera
@TasosPapastylianou thanks for your comments... 1) doesn't work, 2) movie only shows last frame, 3) Haven't tried animated gifs. That's last resort :)Fauver
I have the feeling this will have to wait till 2016b..Fauver
Live scripts do not allow to interact with the plots. I can think of a solution to embed a .gif into an .html exported live script. If that could be of any help I can provide a solution, but would recommend submitting a feature request to the mathworks.Jaques
On a marginally related note, introduced in R2016b is zoom/pan/rotation for live editor figure outputs. It's not yet programmatic (at least overtly), but it's moving in a useful direction.Badger
@excaza ah, it's out.. I wanted to put that into an answer once released ..Fauver
F
0

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.

Fauver answered 4/9, 2019 at 11:23 Comment(0)
V
1

It seems the answer is no - Live Scripts are too young to be that feature rich yet. The fact alone that they're undebuggable would make me stay away from them for 1-2 versions more. Have you looked into Matlab Notebooks? If you're after pretty formatting and some basic interactivity, It might be what you seek.

Viera answered 5/9, 2016 at 10:18 Comment(4)
If I remember correctly, Notebooks are Windows only?Fauver
MS-word only. What OS are you targeting?Viera
Just checked, it's Windows only. I'd like this to work on Windows and Mac, and preferably Linux, too.Fauver
Ok, sorry I can't offer a better answer.Viera
F
0

Release 2016b added the option to manipulate axes with controls that come into view when hovering over the axes. Note that this does not work for axes that are invisible (Visible='off'). Instead, the rulers and background have to be hidden:

ax = axes;
x = rand(9, 3);
plot3(ax,x(:, 1), x(:, 2), x(:, 3), 'x'); 
% Hide rulers and background color
ax.Color = [1 1 1 0]; 
ax.XAxis.Visible ='off'; 
ax.YAxis.Visible ='off'; 
ax.ZAxis.Visible ='off';

Axes arranged with subplot can be manipulated individually as well.

Fauver answered 16/9, 2016 at 12:17 Comment(0)
F
0

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.

Fauver answered 4/9, 2019 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.