Simple GUI question about functions and text boxes.
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (1)
Andrew Reibold
el 7 de Jun. de 2013
Editada: Andrew Reibold
el 7 de Jun. de 2013
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 comentario
Atul Vaibhav
el 13 de Jul. de 2016
Hii Andrew! Can you please tell me how can i call a msgbox by a pushbutton if I m not using Guide? Thanks!
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!