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 = {};
DirNames = {};
FileLabels = {};
SelectedCells;
To remove entries from the table, first I use the UITableCellSelection callback function to update the indices of the selected cells:
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:
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:
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!