How do I get the user selection from a table created using guide. matlab.ui.control.Table
10 次查看(过去 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
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!