App Designer UITable Cell Selection Callback event for deselecting cells

52 views (last 30 days)
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 Comments
Niraj Desai
Niraj Desai on 29 May 2020
I had this same problem in App Designer. The solution was to set the Column Editable property to true.

Sign in to comment.

Answers (2)

Andres Fonts
Andres Fonts on 20 Jun 2019
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 Comments
nada abd
nada abd on 12 Dec 2019
Edited: nada abd on 12 Dec 2019
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 on 16 Apr 2020
@nada abd: You haven't understood at all the topic of the question. Your reply is irrelevant.

Sign in to comment.


Gavriel Aminov
Gavriel Aminov on 9 Mar 2020
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.

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!