Can I open a MATLAB Animation block programmatically?

5 ビュー (過去 30 日間)
William Gray
William Gray 2024 年 2 月 29 日
コメント済み: William Gray 2024 年 3 月 14 日
I am running a Simulink model that uses mouse inputs to control a simple system, with an App Builder application for the front end and a MATLAB Animation block to provide a visual display. Everything works great, but when a user closes the animation figure they need to open the simulink file and double-click on the MATLAB Animation block to re-open it. I would like to automatically open the window from code, both when I load the Simulink model and when a user has inadvertently closed the window. (Via a button on the app.) Can this be done, and if so, how?

回答 (1 件)

R
R 2024 年 3 月 6 日
Hello William,
I assume you are using the MATLAB Animation block from the Aerospace Blockset, which allows you to create six-degrees-of-freedom multibody custom geometry animations. To reopen the simulation window after a user closes it, you need to create an Aero.Animation object and add camera, body, and geometry objects to it. These objects are saved in the workspace after the simulation, and you can find their names and properties by opening the MATLAB Animation Block Parameters dialog box once you simulate your model.
However, note that once the animation window is closed, the animation object is destroyed, and you cannot reopen it. Therefore, you need to create a new animation object every time you want to open the animation window.
Here is a sample code that shows how to create an "Aero.Animation" object and add the camera, body, and geometry objects from the workspace. I have done it for an existing example and you must modify the code according to your specific model and simulation data.
% Create an animation object and set the frames per second and time scaling properties
h = Aero.Animation;
h.FramesPerSecond = 10;
h.TimeScaling = 5;
%Create 5 geometry files and load the geometry files
g1 = Aero.Geometry;
g1.load('astbluewedge.mat');
g2 = Aero.Geometry;
g2.load('astredwedge.mat');
g3 = Aero.Geometry;
g3.load('astredwedge.mat');
g4 = Aero.Geometry;
g4.load('obstacle.mat');
g5 = Aero.Geometry;
g5.load('obstacle.mat');
%Create five body objects and assign the geometry objects to them
b1 = Aero.Body;
b1.Geometry = g1;
b2 = Aero.Body;
b2.Geometry = g2;
b3 = Aero.Body;
b3.Geometry = g3;
b4 = Aero.Body;
b4.Geometry = g4;
b5 = Aero.Body;
b5.Geometry = g5;
%Add the body objects to the animation object
h.addBody(b1);
h.addBody(b2);
h.addBody(b3);
h.addBody(b4);
h.addBody(b5);
%Create camera and set it’s properties and add it to animation obj
c = Aero.Camera;
c.Offset = [0,0,0]; % change this to your desired offset
c.AimPoint = [0,0,0]; % change this to your desired aim point
c.ViewAngle = 10; % change this to your desired view angle
h.Camera = c;
load('simdata.mat'); % change this to your data file name, if any
%Set time series data for each body object
b1.TimeSeriesSource = simdata.b1; % change this to your data structure name if any
b2.TimeSeriesSource = simdata.b2;
b3.TimeSeriesSource = simdata.b3;
b4.TimeSeriesSource = simdata.b4;
b5.TimeSeriesSource = simdata.b5;
%Initialize the animation object and open the animation window
h.initialize();
h.openAnimation();
If you want to add a button in your App Designer app that can open the animation window, you can use the callback function of the button to execute the code. Here is an example of how the callback function might look:
% Button pushed function: OpenAnimationButton
function OpenAnimationButtonPushed(app, event)
%Include all the code in the above section here..
end
For more information, please refer to the following documentation links:
I hope this helps!
  3 件のコメント
William Gray
William Gray 2024 年 3 月 14 日
I appreciate the help but it appears there isn't a way to solve this problem for my application--that I have the skills to use. I have a workaroound that should solve my problem.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSimulation についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by