Working with varialbes in radio button callback
Working with varialbes in radio button callback
(OP)
I am trying to modify a variable Tbread inside a radio button call back. Here is the important parts of the code I have.
I get this error:
??? Undefined function or variable "Tbread".
Error in ==> ElectronPizza>radiobutton1_Callback at 186
Tbread=Tbread-2
Error in ==> gui_mainfcn at 75
feval(varargin{:});
Error in ==> ElectronPizza at 27
gui_mainfcn(gui_State, varargin{:});
??? Error using ==> ElectronPizza('radiobutton1_Callback',gcbo,[],guidata(gcbo))
Undefined function or variable "Tbread".
??? Error while evaluating uicontrol Callback
How do I properly allow this callback to work with this variable? Other callbacks will also be modifying it in a similar manner. Thanks in advance
CODE
function variable()
global Tbread;
Tbread=0;
end
% --- Executes on button press in radiobutton1. 7.50 MEDIUM PIZZA
%medium pizza: 7.50 dollars: 2 bread: 2 suace: 2 cheese
function radiobutton1_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == get(hObject,'Max'))
% Radio button is selected-take approriate action
Tbread=Tbread-2
Tsauce=Tsauce-2
Tcheese=Tcheese-2
total=total+7.50
else
% Radio button is not selected-take approriate action
Tbread=Tbread+2
Tsauce=Tsauce+2
Tcheese=Tcheese+2
total=total-7.50
end
end
global Tbread;
Tbread=0;
end
% --- Executes on button press in radiobutton1. 7.50 MEDIUM PIZZA
%medium pizza: 7.50 dollars: 2 bread: 2 suace: 2 cheese
function radiobutton1_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == get(hObject,'Max'))
% Radio button is selected-take approriate action
Tbread=Tbread-2
Tsauce=Tsauce-2
Tcheese=Tcheese-2
total=total+7.50
else
% Radio button is not selected-take approriate action
Tbread=Tbread+2
Tsauce=Tsauce+2
Tcheese=Tcheese+2
total=total-7.50
end
end
I get this error:
??? Undefined function or variable "Tbread".
Error in ==> ElectronPizza>radiobutton1_Callback at 186
Tbread=Tbread-2
Error in ==> gui_mainfcn at 75
feval(varargin{:});
Error in ==> ElectronPizza at 27
gui_mainfcn(gui_State, varargin{:});
??? Error using ==> ElectronPizza('radiobutton1_Callback',gcbo,[],guidata(gcbo))
Undefined function or variable "Tbread".
??? Error while evaluating uicontrol Callback
How do I properly allow this callback to work with this variable? Other callbacks will also be modifying it in a similar manner. Thanks in advance





RE: Working with varialbes in radio button callback
...
handles.Tbread=0;
...
guidata(hObject,handles);
The last line save all the changes you make at handles structure. So if you add, delete or change the value of a variable inside a function you must save the changes with guidata.
Handles can be seen as a master copy of your variables, where you go anytime you need a variable, so all the variables you want to use must be inside handles structure.
So inside the callback you should modificate Tbread as follows:
handles.Tbread=handles.Tbread-2
...
guidata(hObject,handles);
Good luck