MATLAB Answers

How can I invoke a function from within App Designer when using a DeleteFcn or CloseRequestFcn from a MATLAB figure that is separate from the App GUI?

8 views (last 30 days)
Allen on 23 Aug 2019
Commented: Ganesh Regoti on 22 Oct 2019
I am trying to find a work around to the lack of axes callback usage from within an App Deisnger axes. Ultimately, I want to assign a KeyPressFcn to allow me to manual shift data along the x-axis when pressing left- and right-arrow keys. I am also selectively performing this x-axis shift on only visible lines, were line visibility can be toggled 'on/'off' using the axes legend's ItemHitFcn. Currently, I am copying axes objects to a new figure outside of App Designer were I can acheive this, and can even obtain the new XValues via one of the app's ButtonGUI callbacks, but would much prefer to invoke the button's callback automatically when the figure is closed.
% Figure creation from app
hFig = figure;
hAx = axes;
hLin = plot(hAx,X,Y);
hFig.DeleteFcn = @(app,event) UpdateAxesButtonPushed(app,event,hLin.XData);
Button callback function:
methods (Access = public)
function UpdateAxesButtonPushed(app, event, newX)
% First verifies that the source of the event is the callback
app.UIAxes.Children(1).XData = newX;
Code for an additional attempt is provided code section below in better detail. Also, the error that appears is same for all attempts and is as follows.
Undefined function 'UpdateAxes' for input arguments of type 'matlab.ui.Figure'.
Error using MyApp/UpdateAccPlot (line 221)
Error while evaluating Figure CloseRequestFcn.
methods (Access = public)
function UpdateAccPlot(app, event)
% Create figure with user defined CloseRequestFcn
hFig = figure('WindowState','maximized','CloseRequestFcn',@UpdateAxes);
hAx = axes(hFig);
% Plot data
hLin = plot(hAx,app.Data.Time,app.Data.UnfilteredAcc,...
hLin(1).Color = [0,0.447,0.741];
hLin(2).Color = [0.929,0.694,0.125];
hLeg = legend(hAx,{'Unfiltered Data','Filtered Data',...
'Upper Limit','Nominal Pulse','Lower Limit'});
hAx.XLim = app.Pulse.Time([1,end]);
% Assign callbacks to new figure plot
app.hGUI = hLin;
hLeg.ItemHitFcn = @LineVisible;
hFig.KeyPressFcn = @(src,event) LineShift(src,event,hLin);
grid on
title(hAx,'Shock Pulse Acceleration')
xlabel(hAx,'Time (sec)')
ylabel(hAx,'Acceleration (g)')
app.Data.Time = getappdata(0,'Time');
app.Data.Time = getappdata(0,'Unfiltered');
app.Data.Time = getappdata(0,'Filtered');
function UpdateAxes(~, ~)
hLin = findobj(gcf,'Type','line');
idx{1} = arrayfun(@(a) strcmp(a.DisplayName,'Unfiltered Data'),hLin);
idx{2} = arrayfun(@(a) strcmp(a.DisplayName,'Filtered Data'),hLin);

  1 Comment

Ganesh Regoti
Ganesh Regoti on 22 Oct 2019
Hi Allen,
I tried replicating the same functionality as per my understanding and is working fine for me. It would help me further if you can share the script of app designer and callback functions if possible.

Sign in to comment.

Answers (0)

Sign in to answer this question.