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

7 views (last 30 days)
Eric Osmon
Eric Osmon on 8 May 2022
Answered: Jonas on 9 May 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 Comment
Walter Roberson
Walter Roberson on 9 May 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.

Sign in to comment.

Answers (1)

Jonas
Jonas on 9 May 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

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by