필터 지우기
필터 지우기

How to get value of uicontrol object whose callback is executing?

조회 수: 24 (최근 30일)
Adam Hicks
Adam Hicks 2018년 12월 12일
댓글: Adam Hicks 2018년 12월 12일
I have a GUI with a variable number of uicontrol pushbuttons which share a single callback function. In order to make this work, I am using the value of each of the buttons to determine how to handle the data being given. I assumed I would be able to use get(gcbo,'Value') in the callback function to determine which button was pressed, but for every button it simply returns 1. I have checked that each uicontrol object has a unique value. Here is a simplified example of my code:
handles.browse(i) = uicontrol('Style','pushbutton',...
'String','...',...
'Value',i,...
'TooltipString','Browse for a file.',...
'Position',[5 ypos-20 20 27],...
'Callback', @Browse);
function Browse(varargin)
val = get(gcbo,'Value');
[filename,path,index] = uigetfile('*.mat','Locate File');
if index
output{val} = [path filename];
end
end
Please keep in mind that I am working with version R2010b, though this shouldn't be an issue.
  댓글 수: 1
Adam
Adam 2018년 12월 12일
What information is it you need to know about the button pressed?
gcbo
should return you the unique handle of the button that was pressed. It depends what you then want to do with that information. In your example you are using the 'value' to index into a cell array and store information, which is not ideal as you certainly won't get integers starting from 1 as the results for each button.
You could store your [ path filename ] information in a
doc containers.Map
object where the key is the button handle (as a double).

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

채택된 답변

Jan
Jan 2018년 12월 12일
편집: Jan 2018년 12월 12일
The property 'Value' of a button is either 0 or 1 and you cannot use it to store data. But you can use the UserData, add an input to the callback or compare the handle of the pressed button with the list of button handles:
function main
FigH = figure;
for index = 1:5
handles.browse(index) = uicontrol('Style','pushbutton',...
'String', sprintf('%d', index), ...
'Position',[5 k*22 20 20],...
'Callback', {@Browse, index}, ... % Provide index as 3rd input
'UserData', index); % Alternative 1: UserData
end
% Alternatively 2: store the button handles:
guidata(FigH, handles);
end
function Browse(ButtonH, EventData, index)
disp(index);
% Alternative 1:
index1 = get(ButtonH, 'UserData');
disp(index1)
% Alternative 2:
handles = guidata(ButtonH);
index2 = find(handles.browse == ButtonH);
disp(index2)
end
All three methods let the callback identify, which button was pressed.
By the way, using the handle provided as 1st input of the callback is more direct than requesting gcbo.
  댓글 수: 1
Adam Hicks
Adam Hicks 2018년 12월 12일
I went with the UserData option as it was the easiest to implement into what I currently had. I just replaced 'Value' with 'UserData' and it works exactly as I originally intended. Thanks to Adam Danz who suggested this solution as well.

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

추가 답변 (1개)

Adam Danz
Adam Danz 2018년 12월 12일
"In order to make this work, I am using the value of each of the buttons to determine how to handle the data being given."
Idea 1) The 'value' property describes the state of the button (on/ off) (see this link). It is not the identity of the button. Each button will have a different handle which could be used to identify which button was pressed. However, the handle value will differ each time the GUI is initialized. If the names of your buttons differ, you could use the button names which are accessible by:
val = get(gcbo,'String');
Idea 2) A button identifier value could be stored in the 'tag' or 'UserData' property of each button that could be accessed instead of 'value' or 'string'.
Idea 3) last but not least, each button's callback function could contain a button identifier variable that could be passed to the Browse() function.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by