MATLAB Answers

How to use onCleanup?

3 views (last 30 days)
Uyen Nguyen
Uyen Nguyen on 27 Sep 2020
Answered: Walter Roberson on 27 Sep 2020
I currently have a running app where it calculates the moment of inertia after taking in inputs from user. The outputs are displayed on a separate figure after each calculation. I want to try to use onCleanup to try to close the old figure every time I calculate a new moment of inertia. But I'm confused on where to start.
if app.ShapeDropDown.Value == "Rectangle"
% taking in the dimensions of base and height
b = app.Dimension1EditField.Value;
h = app.Dimension2EditField.Value;
% Catching resulting errors if the input values are invalid
if b < 0 || h < 0
error('Negative Dimension(s) Entered')
b = 0;
h = 0;
errordlg('Invalid Input');
errID = 'myComponent:inputError';
msgtext = 'Negative Inputs';
ME = MException(errID,msgtext)
% Calculate MOI
Ix = (b*(h^3))/12;
Iy = ((b^3)*h)/12;
% Displau the MOI on the table
d = {'Base',b, RegUnit;'Height',h,RegUnit;'Ix',Ix, CalcUnit;'Iy',Iy, CalcUnit'};
fig = uifigure('Position',[100 100 752 250]);
uit = uitable('Parent',fig,'Position',[25 50 700 200]);
uit.Data = d;
% Create axes for the figure. Axes of figure changes
% depending on base and height entered
app.UIAxes.XLim = [-1 b+1.00];
app.UIAxes.YLim = [-1 h+1.00];
%Polar axes
XX = linspace(b/2, Ix, 500);
YY = linspace(h/2, Iy, 500);
% % Create a vector that would span the X & Y direction
% positioned at the center of base and height
IXline = ones(1,500)*h/2;
IYline = ones(1,500)*b/2;
% Graph the rectangular shape on the axes
rectangle(app.UIAxes,'Position',[0 0 b h])
hold(app.UIAxes);%hold on
% plot the polar axes
plot(app.UIAxes,XX,IXline, 'r')
plot(app.UIAxes,IYline, YY, 'k')


Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 27 Sep 2020
You probably would not use onCleanup for that. Instead you would create a property of app that you would initialize to gobjects(1) . I will call that MOIfig in the below. Then inside the routine, when you are ready to plot, you would
if isvalid(app.MOIfig)
fig = uifigure('Position',[100 100 752 250]);
app.MOIfig = fig;


Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by