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
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.
% 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
% set the callbacks
% put some Data in the table for testing:
% handles.tableScores CellSelectionCallback function:
% store the selected cells in the UserData of handles.tableScores
% handles.pushbutton Callback function:
handles = guidata(src);
% get the selected cells in handles.tableScores:
selected_cells = get(handles.tableScores,'UserData')
% then do something with selected_cells ...
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.