Borrar filtros
Borrar filtros

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

24 visualizaciones (últimos 30 días)
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 comentario
Adam
Adam el 12 de Dic. de 2018
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).

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 12 de Dic. de 2018
Editada: Jan el 12 de Dic. de 2018
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 comentario
Adam Hicks
Adam Hicks el 12 de Dic. de 2018
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.

Iniciar sesión para comentar.

Más respuestas (1)

Adam Danz
Adam Danz el 12 de Dic. de 2018
"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.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Productos


Versión

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by