Creating a GUI based calculator
Show older comments
I am trying to create a GUI based calculator, in it I use drop boxes and buttons to assign values to certain input parameters. Using these parameters I need to perform calculations that will be displayed in a static text box within the GUI. What I am having trouble with is displaying the results into static text boxes. I know I should be able to use a set command to do this but it is not working. Any ideas? I can show you my code if you are curious but didn't want to muddle the question up with code.
8 Comments
Geoff Hayes
on 24 Jun 2014
Are you using GUIDE to create your GUI? I'm guessing that there is a callback for a button on your GUI that when pressed, does some calculations and then should update the text boxes. It is within this callback that you will want to call the set function against the different text boxes.
Gregory
on 24 Jun 2014
Geoff Hayes
on 24 Jun 2014
Edited: Geoff Hayes
on 24 Jun 2014
In GUIDE, the signature for the popup callback would be something like
function popup_month_Callback(hObject, eventdata, handles)
I'm not sure why you can't do the same thing in your definition for the popup. Then the body would be something like
function popup_month_Callback(hObject, eventdata, handles)
% get the 1-12 month index for the currently selected item
monthIdx = get(hObject,'Value');
switch monthIdx
case 1
days = 0;
case 2
days = 31;
% etc.
end
% set the text field
set(handles.editTextField,'String',num2str(days));
Note the switching on the month index rather than the string (could do the same).
Gregory
on 24 Jun 2014
Geoff Hayes
on 24 Jun 2014
It is not standalone code - it must be invoked/called in response to something which is presumably the change in the month from the pop-up menu.
In the above code example, which is for the popup month callback, it fires in response to the user selecting a month. From that month, we get the number of days, and that is (I'm guessing) the number you want to display in your (static) text field.
The line of code, set(handles.editTextField,'String',num2str(days)); is a line of code within the popup_month_Callback function.
Gregory
on 24 Jun 2014
Geoff Hayes
on 24 Jun 2014
I'm using the handle to the control, handles.editTextField, to set its string value to num2str(days). In GUIDE, the handles structure contains a list of (among other things) the handles to all widgets (text boxes, popup menus, sliders, axes, etc.)
My example may illustrate a difference between using GUIDE and you yourself writing the GUI code. Check out Joseph's solution below which, while similar, will work for your case.
Gregory
on 24 Jun 2014
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!