필터 지우기
필터 지우기

How do I get the user selection from a table created using guide. matlab.ui.control.Table

조회 수: 20 (최근 30일)
I have a complex plotting tool written with a gui using guide. Within that gui are multiple objects, one of which is a table object called 'tableScores'.
I want the user to be able to select parts of that table, then click a command button, then a scrips will run that plots selected values from that table. That's all quite simple.
However, within the command buttons callback, I can't work out how to access the users selected cells from the table.
I can get handles.tableScores.Data which is the tables data.
handles.tableScores.Selection exists, but is empty, despite the user having selected cells within the table.
I just can't find the name of the property for user selection within a table.
I'm using MATLAB 2020a

채택된 답변

Voss
Voss 2022년 7월 13일
편집: Voss 2022년 7월 13일
"I just can't find the name of the property for user selection within a table."
As far as I can tell, a uitable doesn't have such a property.
However, you can set a CellSelectionCallback on your uitable, in which you can store the selected cells. Store them in some variable you can access where you need them, or store them in the UserData of the uitable as I do below.
Example:
% create a figure with a uitable and a pushbutton
% (using a handles structure to emulate what you have in GUIDE)
handles.figure = figure();
handles.tableScores = uitable();
handles.pushbutton = uicontrol('String','OK');
% store the handles structure
guidata(handles.figure,handles);
% set the callbacks
set(handles.tableScores,'CellSelectionCallback',@csf_table);
set(handles.pushbutton,'Callback',@cb_pb);
% put some Data in the table for testing:
set(handles.tableScores,'Data',magic(5));
% handles.tableScores CellSelectionCallback function:
function csf_table(src,evt)
% store the selected cells in the UserData of handles.tableScores
set(src,'UserData',evt.Indices);
end
% handles.pushbutton Callback function:
function cb_pb(src,evt)
handles = guidata(src);
% get the selected cells in handles.tableScores:
selected_cells = get(handles.tableScores,'UserData')
% then do something with selected_cells ...
end
You can copy that code into a script and run it, make sure selected_cells seems to be what you expect, and then adapt the relevant parts of the code for use in your GUI.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by