App Designer UITable Cell Selection Callback event for deselecting cells

조회 수: 44 (최근 30일)
Jesse Benck
Jesse Benck 2018년 8월 10일
답변: Ahmed Rashad 2024년 8월 8일
I have an App Designer GUI that includes a UITable which displays names and labels for a list of files that I have selected. I also have some buttons to add or remove files from the table. All of this works well, except for one issue with the "remove files" button.
The data for each file displayed in the table is stored in variables which are initialized in the properties section. I also have a SelectedCells variable that contains the indices of the selected cells in the UITable:
FileNames = {}; % Current list of files
DirNames = {}; % Current list of directories corresponding to each file
FileLabels = {}; % File labels
SelectedCells; % Rows selected in the table of file names
To remove entries from the table, first I use the UITableCellSelection callback function to update the indices of the selected cells:
% Cell selection callback: UITable
function UITableCellSelection(app, event)
app.SelectedCells = event.Indices;
end
Then, I use a "remove files" button pushed callback function to delete the appropriate entries and update the table:
% Button pushed function: RemoveSelectedFilesButton
function RemoveSelectedFilesButtonPushed(app, event)
if ~isempty(app.SelectedCells)
NumSelected = size(app.SelectedCells,1);
for i = 1:NumSelected
Rows(i) = app.SelectedCells(i,1);
end
UniqueRows = unique(Rows);
for i = 1:length(UniqueRows)
app.FileNames(UniqueRows(i),:) = [];
app.DirNames(UniqueRows(i),:) = [];
app.FileLabels(UniqueRows(i),:) = [];
end
app.NumFiles = app.NumFiles - length(UniqueRows);
app.UITable.Data = [app.FileNames app.FileLabels];
app.SelectedCells = [];
end
end
This works as expected (i.e. deletes the entries) if I have any cells selected. It also works fine if I have never selected any cells at all (i.e. it does nothing). The problem happens when I first select some cells, but then click out of the selection so that there are no longer any cells highlighted. When I do this, as far as I can tell, the UITableCellSelection callback function is not triggered, so the app.SelectedCells variable is not updated. This means that when I click "remove files", whatever files were most recently selected will be deleted, even though they are not actually selected anymore.
To troubleshoot, I tried tweaking the UITableCellSelection function a little bit, which helped me to understand the problem, but did not resolve it:
% Cell selection callback: UITable
function UITableCellSelection(app, event)
app.SelectedCells = event.Indices;
if isempty(event.Indices)
app.SelectedCells = [];
end
disp(app.SelectedCells)
disp('selection change')
end
This does not fix the problem, but it helped me see that there is no selection change triggered when the cells are deselected.
So, is there a way to detect the deselection event, and clear the app.SelectedCells variable? Or is there a better way to produce the desired functionality?
Thank you!
  댓글 수: 4
Jesse Benck
Jesse Benck 2019년 6월 20일
Unfortunately I am still not aware of a solution.
Niraj Desai
Niraj Desai 2020년 5월 29일
I had this same problem in App Designer. The solution was to set the Column Editable property to true.

댓글을 달려면 로그인하십시오.

답변 (3개)

Andres Fonts
Andres Fonts 2019년 6월 20일
Hi!
I am trying a very similar thing.
I added a "Remove data" button, then I remove the appropiate row of the table using the app.SelectedCells variable, same as you.
Then I wirte the table with blank data, and after that I write the data I want:
data = app.UITable.Data;
row = app.SelectedCells(:,1);
data(row,:) = [];
app.UITable.Data = [];
app.UITable.Data = data;
  댓글 수: 3
nada abd
nada abd 2019년 12월 12일
편집: nada abd 2019년 12월 12일
Hello all;
To remove a row from selection, try this code below:
% Cell selection callback: UITable
function UITableCellSelection(app, event)
indices = event.Indices
data = app.UITable.Data;
row = indices(:,1);
app.UITable.Data(row,:) = [];
end
Chris Oreinos
Chris Oreinos 2020년 4월 16일
@nada abd: You haven't understood at all the topic of the question. Your reply is irrelevant.

댓글을 달려면 로그인하십시오.


Gavriel Aminov
Gavriel Aminov 2020년 3월 9일
I also have had the problem of deselecting table cells and I wonder why there is no appropriate Callback for the Deselecting event, as symmetric requirement for SelectCells one.
My bypass to the probelm is as follows and sorry for the scanned code.
  1. The Callback that treats Deselecting is the ButtonDown Callback of the App Main Figure, and it is invoked when the user clicks on the empty area of the figure.
  2. The callback then consists of two parts: unlighting the cells and cleaning the global list of selected cells. The unlighting is done by deleting and restoring the Data field of the Table.
  3. In order to avoid clicking on the empty area of the Table, which has no ButonDown callback as for R2019a, we update the height of the Table dynamically with each refresh of the Table.

Ahmed Rashad
Ahmed Rashad 2024년 8월 8일
I found a simple solution to this problem. in your CellSelection callback add the if condition
if isempty(event.Indices)
else
"do stuff"
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