필터 지우기
필터 지우기

how to get uicontrol value inside a callback function?

조회 수: 9 (최근 30일)
lou
lou 2015년 9월 23일
댓글: Walter Roberson 2015년 9월 24일
Hi, I have a have written a function which has several callback functions. My function acts on a figure. I have added a uicontrol button to my figure and this is where my problem starts.
I would like to get the value of the GUIbutton state while I am still inside another callback function.
%==another callback function===
button1 = uicontrol('Style','pushbutton', 'Value', f,'pos',...
[20 10 10 10],'string','text_here');
state=get(handles.button1, 'Value');
if state==1;%button is clicked
%do something
else
%do something else
end
%===another callback function====
this doesnt work, because the class handles is undefined, but unfortunately I am unable to understand how to fix it.

채택된 답변

Walter Roberson
Walter Roberson 2015년 9월 23일
Functions cannot have callbacks. Objects (especially graphic objects) can have callbacks.
  댓글 수: 2
lou
lou 2015년 9월 24일
Yes, you're right. My created figure has several callback functions, not the function. I have added a togglebutton to my figure and I would like to extract the state of the togglebutton, but I do not know how to do this correctly when the state of the togglebutton needs to be extracted inside another callback function.
Walter Roberson
Walter Roberson 2015년 9월 24일
편집: Walter Roberson 2015년 9월 24일
If you are using GUIDE, then you get() the Value property of the handles structure entry named the same thing as the Tag of the toggle button. For example,
get(handles.togglebutton1, 'Value')
If you are not using GUIDE or you are creating the toggle button dynamically then you need to use one of the techniques for sharing data from the link I posted.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2015년 9월 24일
편집: Stephen23 2015년 9월 24일
The easiest way would be to use nested functions, then the variable handles is visible inside the callback workspaces as well, without needing to pass it explicitly:
function main
handles.cancelBut = ...
...
function callback_cancel_button
get(handles.cancelBut,'Value')
end
end
Otherwise you should read the documentation, which describes all of the ways to pass variables (e.g. your handles structure) between callbacks:
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 9월 24일
If you are using GUIDE, then this is not the easiest way, as GUIDE historically does not terminate each function with a matching "end" statement as is required for all functions in the same file if you want to use nested functions.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by