How can I create a button to return the indices of a uitable selection?

2 vues (au cours des 30 derniers jours)
max norris
max norris le 16 Mai 2017
Hello,
I have created a simple uitable to display the contents of an N x 1 array, A: g=uitable(f,'Data',A, ....). In essence it is a menu of things to choose from. I would like to select values from the uitable and create a uicontrol button to return the indices of my selection.
I have seen multiple entries for things like event.Indices, CellSelectionCallback, etc. but I am having a hard time understanding it.
Thank you in advance

Réponses (1)

Astarag Chattopadhyay
Astarag Chattopadhyay le 22 Mai 2017
Hi,
Here is an example script that creates an 'uitable' with a 'CellSelectionCallback' that retrieves the indices of the selected cells. Further, on the callback for the pushbutton, the values are displayed on the screen.
%function example
%Doc example for uitable
f = figure('Position',[100 100 400 150]);
dat = {6.125, 456.3457;...
6.75, 510.2342;...
7, 658.2;};
columnname = {'Rate', 'Amount'};
columnformat = {'numeric', 'bank'};
columneditable = [false false];
t = uitable('Units','normalized','Position',...
[0.1 0.1 0.9 0.9], 'Data', dat,...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'ColumnEditable', columneditable,...
'RowName',[],...
'CellSelectionCallback',@mycb); %Add cellselectioncallback
%Add pushbutton
uicontrol('style','pushbutton','callback',@(~,~)pbcb(t),... %give handle to table
'units','normalized','position',[0 0 0.4 0.2],'string','Return Selection');
end
function mycb(src,evt)
set(src,'UserData',evt.Indices); %Make indices available to everyone through the UITABLE's userdata
end
function pbcb(t)
for i=1:size(t.UserData,1)
x(i)=t.Data(t.UserData(i,1),t.UserData(i,2));
end
if size(x,1)==0
error('Please select the Data');
else
disp(x);
end
end

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by