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

9 views (last 30 days)
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

Accepted Answer

Voss
Voss on 13 Jul 2022
Edited: Voss on 13 Jul 2022
"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.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by