sharing data between GUİ functions?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Geoff Hayes
2015년 5월 13일
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
Image Analyst
2015년 5월 13일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!