App - changing button state when a uifigure is closed

9 views (last 30 days)
When the user presses a state button, a UIfigure is generated. When the user closes that UIfigure, I want to reset the state of the button. I think I have a syntax error somewhere- any assistance is much appreciated. First is the code for the button press, next is the code for the function.
%T0 Switch on/off recording of script
if app.RecordScriptButton.Value==1
app.RecordScriptButton.BackgroundColor='green';
app.RecordScriptButton.Text='Recording';
app.ScriptContent={'%Paste the following text in the main workspace and press <enter> to run the script.'};
app.ScriptWindow= uifigure('Position',[100 100 420 420],'Name','Script');
set(app.ScriptWindow,'CloseRequestFcn',closeScriptFcn);
app.ScriptTextArea = uitextarea(app.ScriptWindow,'Value', app.ScriptContent,'Position',[10 10 400 400],'FontSize',12,'FontWeight','bold');
else
app.RecordScriptButton.BackgroundColor='red';
app.RecordScriptButton.Text='Record Script';
%Close window and erase the script for next use - use try in case user has already closed window
try
close(app.ScriptWindow);
end
end
%function code, located at start of app code.
function closeScriptFcn(app)
app.RecordScriptButton.Value=0;
app.RecordScriptButton.BackgroundColor='red';
close(app.ScriptWindow);
end
  5 Comments
Jessica Hiscocks
Jessica Hiscocks on 12 Jul 2018
Edited: Jessica Hiscocks on 12 Jul 2018
Changed the function code as per below. No display commands ran, so the function isn't even being called properly. It's possible this has something to do with the way functions are called in the app, functionname(app) is the usual syntax. But that doesn't work either.
function closeScriptFcn(app)
disp('Function Activated')
app.RecordScriptButton.Value=0;
disp('Script button reset')
app.RecordScriptButton.BackgroundColor='red';
disp('button coloured')
close(app.ScriptWindow);
disp('Script window closed')
end
Geoff Hayes
Geoff Hayes on 13 Jul 2018
I haven't used App Designer so can't comment on why this might not be working. I suppose that when you create the figure, you could assign the callback there
app.ScriptWindow= uifigure('Position',[100 100 420 420],'Name','Script', 'CloseRequestFcn',@closeScriptFcn);
though I'm not sure if that will make a difference. You can confirm that
close(app.ScriptWindow);
is being called? And there aren't any other warnings or errors in the command window that might explain why the CloseRequestFcn is not being called?

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 26 Jul 2018
A perfect case for events and listeners. Add a listener for the ObjectBeingDestroyedEvent of the new uifigure.
app.ScriptWindow = uifigure;
addlistener(app.ScriptWindow, 'ObjectBeingDestroyed', @(~,~)disp('hello world'))
Replace the disp statement with setting your button state to the desired.
  5 Comments
Sean de Wolski
Sean de Wolski on 27 Jul 2018
Typo: addlistener not addlistner. However, since MathWorks is headquartered outside of Boston, we should have "addlisnah"!
Jessica Hiscocks
Jessica Hiscocks on 27 Jul 2018
Thank you, this worked. At which point I realised I actually had several commands that needed to execute when the window closed, so I needed to call a function instead. With the syntax you gave me above, I managed to get that to work too. For anyone else's future reference, that is as per below:
addlistener(app.ScriptWindow,'ObjectBeingDestroyed',@(~,~) closeScriptFcn(app,4));
For a function that starts
function closeScriptFcn(app,~)
The 4 is a dummy variable since the app function needs two arguments, and the addlistener doesn't seem to like having a ~ there. Thank you again for sticking it out with this problem.

Sign in to comment.

More Answers (1)

Dennis
Dennis on 13 Jul 2018
I would actually expect a couple of error messages.
app.ScriptWindow= uifigure('Position',[100 100 420 420],'Name','Script');
1.) If ScriptWindow does not already exist at this point, you should not be able to add ScriptWindow to the existing app structure in appdesigner ('Unrecognized proberty 'ScriptWindow'...).
2.) Consider this would work!
You never actually update app. When you run your function a second time (else part) app.ScriptWindow either does not exist or does not point to your uifigure.
3.) Callback functions in MATLAB always need atleast 2 input arguments:
function closeScriptFcn(app,~)
%code
end
If this function was ever called you should have received 'Not enough input arguments'.
I want to suggest an alternative route:
value = app.Button.Value;
if value==1
ScriptWindow=uifigure();
setappdata(app.Button,'h',ScriptWindow)
waitfor(ScriptWindow)
app.Button.Value=0;
else
ScriptWindow=getappdata(app.Button,'h');
close(ScriptWindow)
end
This should work, and does not require another callback function. You could call a function after waitfor().
  1 Comment
Jessica Hiscocks
Jessica Hiscocks on 26 Jul 2018
Hello Dennis, sorry for the late reply. To address your points in order. 1. This line causes the app to open a new window, separate from the app GUI, which is the desired behaviour. 2. Since this is a new window, there's no need to update anything. 3. This is indeed an error in the code, so I've fixed that.
Your code seems like it will close the window if the button state is changed. I have already set that function up, what I need is the opposite: if the Window is closed, I need to change the button state.
If I'm missing something here or you have any suggestions, please let me know.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!