How do I extract data from a uicontrol edit field within an app?

13 Ansichten (letzte 30 Tage)
Eric Osmon
Eric Osmon am 8 Mai 2022
Beantwortet: Jonas am 9 Mai 2022
I'm trying to create a custom dialog box that will return the text the user typed into the edit field when they press "confirm". The dialog box itself is being run in the callback function for a button within an app being pressed. Here's the code I'm currently using for the dialog box:
d = dialog('Position',[710 390 500 225],'Name','Team Name');
txt = uicontrol('Parent',d,...
'Style','text',...
'Position',[25 100 450 100],...
'String','Enter your team name, or press cancel.',...
'FontSize',18);
teamname = uicontrol('Parent',d,...
'Style','edit',...
'Position',[25 100 450 50],...
'FontSize',18);
btn1 = uicontrol('Parent',d,...
'Position',[25 25 212 50],...
'String','Confirm',...
'FontSize',18,...
'Callback','team = teamname.String; delete(gcf)');
btn2 = uicontrol('Parent',d,...
'Position',[263 25 212 50],...
'String','Cancel',...
'FontSize',18,...
'Callback','delete(gcf)');
uicontrol(teamname);
This code was working exactly how I wanted when I ran it in a standalone .m file, but when I try to run it in the app, it throws this error:
"Invalid or deleted object.
Error while evaluating UIControl Callback."
What do I need to change for this code to work properly within an app?
  1 Kommentar
Walter Roberson
Walter Roberson am 9 Mai 2022
"app"? It is usually a bad idea to mix traditional figures such as dialog() and gcf() with uifigure based App Designer.
It is not a good idea to rely on using character callbacks and count on them executing commands in the base workspace.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jonas
Jonas am 9 Mai 2022
does this work better for you:
d = dialog('Position',[710 390 500 225],'Name','Team Name');
global team;
team='';
txt = uicontrol('Parent',d,...
'Style','text',...
'Position',[25 100 450 100],...
'String','Enter your team name, or press cancel.',...
'FontSize',18);
teamname = uicontrol('Parent',d,...
'Style','edit',...
'Position',[25 100 450 50],...
'FontSize',18);
btn1 = uicontrol('Parent',d,...
'Position',[25 25 212 50],...
'String','Confirm',...
'FontSize',18,...
'Callback',@(~,~) setTeam(d,teamname));
btn2 = uicontrol('Parent',d,...
'Position',[263 25 212 50],...
'String','Cancel',...
'FontSize',18,...
'Callback',@(~,~) delete(d));
function setTeam(dialog,teamname)
global team
team = teamname.String;
delete(dialog);
end

Kategorien

Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by