App - changing button state when a uifigure is closed
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jessica Hiscocks
le 12 Juil 2018
Commenté : Jessica Hiscocks
le 27 Juil 2018
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 commentaires
Jessica Hiscocks
le 12 Juil 2018
Modifié(e) : Jessica Hiscocks
le 12 Juil 2018
Geoff Hayes
le 13 Juil 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?
Réponse acceptée
Sean de Wolski
le 26 Juil 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 commentaires
Sean de Wolski
le 27 Juil 2018
Typo: addlistener not addlistner. However, since MathWorks is headquartered outside of Boston, we should have "addlisnah"!
Plus de réponses (1)
Dennis
le 13 Juil 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().
Voir également
Catégories
En savoir plus sur Develop uifigure-Based Apps dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!