Problem subtracting and adding a value to a textbox using a popupmenu and push buttons

3 views (last 30 days)
My overall goal is to make a popupmenu with different categories subtracted from a specific text box of 7 total text boxes. I have 7 edit text boxes and 7 categories for each; with each edit text box having a value. I want the user to select the category with a popup menu and then have a new edit text box to enter the subtracted amount. Lastly to restate, I want to subtract the amount from the selected category and update the category textbox with new amount.
Here is what I have tried to do. Matlab is giving me an error for my subtraction button callback. It is specifically not liking the popVal.
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
contents = cellstr(get(hObject,'String'));
popChoice = contents{get(hObject,'Value')};
assignin('base','popChoice',popChoice);
if (strcmp(popChoice,'Food'))
popVal = 1;
elseif (strcmp(popChoice,'Gas'))
popVal = 2;
elseif (strcmp(popChoice,'Invest'))
popVal = 3;
elseif (strcmp(popChoice,'Other'))
popVal = 4;
elseif (strcmp(popChoice,'Clothes'))
popVal = 5;
elseif (strcmp(popChoice,'Emergency'))
popVal = 6;
elseif (strcmp(popChoice,'Extra'))
popVal = 7;
end
assignin('base','popVal',popVal);
guidata(hObject,handles);
% --- Executes on button press in subtract. CALLBACK FOR SUBTRACT BUTTON
function subtract_Callback(hObject, eventdata, handles)
if popVal == 1 % line 557 program not reading popVal
a = str2double(get(handles.food,'String'));
b = str2double(get(handles.amount,'String'));
c = a - b;
set(handles.food,'String',c);

Answers (1)

Santhana Raj
Santhana Raj on 9 May 2017
The GUI workspace handles variables differently than a normal function. The function subtract_callback has its own workspace and this ws doesnt have popVal.
Instead of using assignin, I have used the structure handles. I would define handles.popVal. and then use handles.popVal for comparison in the next function. Note that handles structure is being passed on to all functions in the GUI. THis is one way to overcome this problem.

Categories

Find more on App Building 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!