sharing data between GUİ functions?

4 views (last 30 days)
hii,
i have been created a matlab gui application using checkbox and pushbutton i wanted to guide my push button to run two different .m file according to the Value of checkbox but however i am insistently getting an error: undefined function or variable "state" but when i define state for push button then it forgets totally about the "state" in checkbox how to handle it ? my guess is i havent put some code that will provide sharing data among them if i am right, then what will be that code ? Thank you for your help
my code;
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
state = get(hObject, 'Value');
if state1 == 0
display('unchecked');
else
display('checked');
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if uicontrol(state, 'Value', 1);
run(A.m);
else
run(B.m);
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 13 May 2015
Atas - use the handle structure which has the handles to all of the GUI controls. In your case you will want to do something like the following in order to get the state of checkbox1 from within the pushbutton1
function pushbutton1_Callback(hObject, eventdata, handles)
state = get(handles.checkbox1, 'Value');
if state == 1
run('A.m');
else
run('B.m');
end
Note how the inputs to the run function have been converted to strings (you may need the full path to these scripts if they are not in the MATLAB search path).
  3 Comments
Image Analyst
Image Analyst on 13 May 2015
I'd just do
if state
A;
else
B;
end
That should work unless I've overlooked something.
Like Goeff says the handles structure has info on all the GUI controls. Consequently, I NEVER use hObject despite the comments telling you to do that. That's probably what you did. And you probably stored state locally in one of the callbacks, and when you were in the other callback "state" was out of scope and didn't see it. But no matter, with handles, you can get the properties of any widget in any callback, so just simply ask for it whenever you need it using the known name of the checkbox.
Atas Fet
Atas Fet on 14 May 2015
Thank you all for your enlightening opinions,
I was using hObject instead of handles structure that was why i had that error.Now ı handle it appriciatte you all.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!