Simple GUI question about functions and text boxes.

2 次查看(过去 30 天)
Hi everyone.
I want to create a GUI with a couple of editable text boxes. Once a value is inputted into both textboxes and a push button is pressed, a function will run computing the two values together relative to that function, and display the answer in a static text box.
I have the basic layout using GUIDE. I just need to know how to manipulate the code into making the values in the text boxes compute according to a certain function and displaying the result.
If anyone could show me how to do this I'd be very grateful.
Thank you.

回答(1 个)

Andrew Reibold
Andrew Reibold 2013-6-7
编辑:Andrew Reibold 2013-6-7
Your push button should have a "Callback Function" generated within the code.
You need to set the "tag" properties of your editable text boxes (There is a tag by default too, but creating a tag is good practice)
If you do not know how to do that, let me know.
After you have the tag properties set, you can call the information that the user entered in the editable text boxes.
Say the tag of your first text box is "input1" and the second is "input2"
The code you would use is
input1str = get(handles.input1, 'String');
input2str = get(handles.input2, 'String');
After this code runs, input1str and input2str are strings of what were entered in the editable text boxes.
If you need them as numbers, use the following code afterwards
input1num = str2num(input1str);
input2num = str2num(input2str);
All of this is happening in the pushbutton callback, and it will activate when you press the button.
Now under the push button callback after this, you can add to the code to do whatever you want with those to inputs. For example, you add them together.
answer = input1num + input2num;
Now if you want to display this answer to the screen, you should add a static text box to your GUI to display it in, or you can make it pop up using the msgbox command.
Say you set the 'tag' property of your static textbox to "answerbox"
The code you would then need to use is
set(handles.answerbox, 'String', answer)
Remember, all of this code should be under the pushbuttons callback, and it all runs when you press the push button.
Let me know if I should clarify anything!
-Andrew
  1 个评论
Atul Vaibhav
Atul Vaibhav 2016-7-13
Hii Andrew! Can you please tell me how can i call a msgbox by a pushbutton if I m not using Guide? Thanks!

请先登录,再进行评论。

类别

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