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

3 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2022-5-9
"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.

请先登录,再进行评论。

回答(1 个)

Jonas
Jonas 2022-5-9
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

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by