Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with varialbes in radio button callback

Status
Not open for further replies.

Five9dak

Mechanical
Nov 28, 2006
11
I am trying to modify a variable Tbread inside a radio button call back. Here is the important parts of the code I have.
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

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
 
Replies continue below

Recommended for you

When you work with Guide all variables you want to use must be defined in handles structure. If you want to use variable Tbread, you should define it in the Opening_Function of your Guide Program, for example.
...
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor