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

조회 수: 1 (최근 30일)
max norris
max norris 2017년 5월 16일
답변: Astarag Chattopadhyay 2017년 5월 22일
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

답변 (1개)

Astarag Chattopadhyay
Astarag Chattopadhyay 2017년 5월 22일
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

카테고리

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