Creating a GUI based calculator

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

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.
I am doing it programmaticly. The online version doesn't have GUIDE and I am at work. I think I am using the callback function incorrectly though as the Mathworks example isn't clear on how to use it for situations outside their example.
I am trying to find the number of days that have passed given a particular month in a popout menu. To do this I use
function popup_month_Callback(days)
if 'Jan'
days=0;
elseif 'Feb'
days=31;
etc.
Which is probably totally incorrect. I'm obviously pretty new at GUI stuff.
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).
People keep showing me the
set(handles.editTextField,'String',num2str(days));
But I am not sure where in the code to put it. Is it standalone or does it go inside one the uicontrol after 'callback'?
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.
That makes sense. But if it is in a callback function how do you call the uicontrol to display set(handles.editTextField,'String',num2str(days)).
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.
Awesome, thank you so much for the help.

Sign in to comment.

 Accepted Answer

Joseph Cheng
Joseph Cheng on 24 Jun 2014
Most-likely you are not setting the return calculations as strings when setting them to the static text.
set(handles.statictexttag,'String',num2str(2));

9 Comments

Gregory
Gregory on 24 Jun 2014
Edited: Gregory on 24 Jun 2014
I've tried using
staticText=uicontrol('Style','text', 'String', num2str(days),'Position',[150,170,125,15]);
within the GUI creating function while setting days as a global variable.
I have also tried using
set(handles.staticText, 'String', num2str(days));
also within the GUI creating function. I'm not sure if that helps explain what is going on any better. I am new to programming GUI.
if you're new i would entirely suggest going with the GUIDE tool to create GUIs. With this tool you can draw out the GUI.
however i hope this quick example should help you out here.
figure,
staticText = uicontrol('style','text','string',datestr(now),'Position',[150,170,125,15]);
staticbutt = uicontrol('style','pushbutton','string','pushme','Position',[150,100,125,15],'callback','set(staticText,''String'',datestr(now))');
I created a quick example where by the push of a button it updates the static text field with the current date and time. (don't hammer the button as it only shows seconds, give it a sec or two so you can see the change)
I know that is what is suggested but the online version doesn't have the option and I'm trying to get a head start on some homework at work, which might be the worst reason for anything ever.
I'm starting to think the problem is in how I am collecting the data. I am trying to find the total number of days that have occurred in the year so far based on the month. To bring in the month I use
hmonth=uicontrol('Style','popupmenu','String',{'Jan', 'Feb', 'Mar','Apr', 'May', 'Jun','Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'}, 'Position',[50,280,100,25]);
Maybe I am using the Callback function wrong...
function popup_month_Callback(days)
if 'Jan'
days=0;
elseif 'Feb'
days=31;
....etc.
The example for that wasn't very clear on how callback functions are supposed to work.
ah so the issue isn't changing the text but more of changing the text due to other inputs without GUIDE type gui programming.
Yes, exactly, it's a calculator. It outputs values based on what you input into the GUI. I am trying to make it output the number of days based on the month you select, eventually combining that with days, and from there a bunch of other functions. At the moment, however, the calculations are secondary to making the program actually show what I need it to.
if you check out the documentation on uicontrol (linked) it kind of shows you how to use the functions. (change swapping of plotting to calculation and return.
here is a quick example of selecting the month and get the number of days in the month (excluding leap year). save the whole thing as a function called guitest.m and run it. as you can see from using the callback i needed to send in the handle object for the static text. otherwise when inside the setmonth function it doesn't "see" the handle object.
function guitest()
hmonth=uicontrol('Style','popupmenu','String',{'Jan', 'Feb', 'Mar','Apr', 'May', 'Jun','Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'}, 'Position',[50,280,100,25]);
staticText = uicontrol('style','text','string',datestr(now),'Position',[150,170,125,15]);
set(hmonth,'Callback',{@setmonth,staticText})
function setmonth(hObj,event,text)
val = get(hObj,'Value')
switch val
case {1,3,5,7,8,10,12}
numdays = 31;
case 2
numdays=28;
otherwise
numdays = 30;
end
set(text,'string',num2str(numdays))
Its really strange because your code works. But i copy pasted the line
staticText = uicontrol('style','text','string',datestr(now),'Position',[150,170,125,15]);
into my program and it is giving me syntax errors or something.
I got it to work I think. But one or two more questions, if you would be so kind. I want to pull 'days' into a function that would calculate other values. Can I do that using a global variable or will that not change as the GUI is changed, is there a better way to do that? I also want to return other values calculated in the function that does actual calculations. Can I do that straight from there or do I need to use another callback function?
Joseph Cheng
Joseph Cheng on 24 Jun 2014
Edited: Joseph Cheng on 24 Jun 2014
I would get out of using global variables. I would suggest using the guidata() function http://www.mathworks.com/help/matlab/ref/guidata.html which you store your variables in the guidata. if you look at the section for calling it within a function (using myhandles = guidata(gcbo)) you can then store values as myhandles.days and save it with guidata(gcbo,myhandles). This way you can call it from different functions without having to figure out how to pass it.
So using this method you can store anything you want under the structure myhandle (or anything else you want to call it)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 24 Jun 2014

Edited:

on 24 Jun 2014

Community Treasure Hunt

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

Start Hunting!