×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Working with varialbes in radio button callback

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.
 

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

RE: Working with varialbes in radio button callback

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources