How do I get a pushbutton to display information a user has input into an "edit" field in a GUI?

2 次查看(过去 30 天)
I can easily get this to work using popup dialogues, though I cannot seem to get this to work when I build a simple GUI from scratch. I can ALSO get this to work when I use GUIDE, but when I do it by hand, something fails.
Here is the code that I have written so far:
function window
%Generate the actual window prompt
name_window = figure;
set(name_window, 'MenuBar', 'none',...
'ToolBar', 'none',...
'Name','Please Enter Your Name',...
'NumberTitle','off')
%Add in text box and submit buttons
submitbtn = uicontrol('Style','pushbutton',...
'String','Submit',...
'Position',[235 190 100 40],...
'Callback', @submitbtn_Callback);
%Add in the text editing field
nameinput = uicontrol('Style', 'edit',...
'Position',[235 250 100 40],...
'Callback', @nameinput_Callback);
end
function submitbtn_Callback(hObject, eventdata, handles)
%Define "name" varible given the text field input
name = char(get(handles.nameinput_Callback, 'String'));
disp(name);
end
I am trying to figure out why my code is not working... I keep getting the same couple of error messages for everything that I try. Usually I am getting:
"Undefined function 'nameinput_Callback' for input arguments of type 'matlab.ui.control.UIControl'.
Error while evaluating UIControl Callback
Not enough input arguments.
Error in window>submitbtn_Callback (line 21) name = char(get(handles.nameinput_Callback, 'String'));
Error while evaluating UIControl Callback"
What exactly am I doing wrong? I've been reading through the code that was generated by GUIDE when I attempted this earlier, but I cannot seem to find anything that would help me.
All I want is that, when a user types something into the edit field and then clicks "Submit", the text that they entered in the edit box is then printed in the command window. But I cannot seem to get it to saved to the name variable.

回答(1 个)

Walter Roberson
Walter Roberson 2016-2-2
Remove
'Callback', @nameinput_Callback
from your definition of nameinput
However, you have the problem that your define
function submitbtn_Callback(hObject, eventdata, handles)
and you do use handles in the body of that function, so three parameters need to be passed to that routine. But
'Callback', @submitbtn_Callback
says that only two parameters should be passed to the routine -- the hObject and eventdata for the callback. MATLAB does not pass three parameters to callbacks automatically; GUIDE cheats and programs a different actual callback property that pulls out the current version of handles and then calls the callback routine with that and the other two parameters.
Unfortunately for you, with the code you have designed, there is no way to know where to fetch the handle structure from. The handle structure is not global: there is one handle structure per GUI, but a GUI might involve multiple figures.
The good news on that front is that because you created your boxes dynamically, they are not in the handles structure anyhow. So you can program around that fact you don't know where the handle structure. And the way to do that is to construct the edit field first and pass its handle when constructing the pushbutton callback:
function window
%Generate the actual window prompt
name_window = figure;
set(name_window, 'MenuBar', 'none',...
'ToolBar', 'none',...
'Name','Please Enter Your Name',...
'NumberTitle','off')
%Add in edit box and submit buttons
%edit box before button
nameinput = uicontrol('Style', 'edit',...
'Position',[235 250 100 40]);
%so that the handle of the edit box is known to pass to the pushbutton callback
submitbtn = uicontrol('Style','pushbutton',...
'String','Submit',...
'Position',[235 190 100 40],...
'Callback', {@submitbtn_Callback, nameinput});
end
function submitbtn_Callback(hObject, eventdata, nameinput)
%Define "name" variable given the text field input
name = char(get(nameinput, 'String'));
disp(name);
end

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by