App Designer UITable Cell Selection Callback event for deselecting cells

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

As a quick follow up, I thought of one potential solution, but I don't know how to implement it (or if it is possible).
Can I somehow determine which cells in the UI Table are currently highlighted? This information must be stored in some variable somewhere, but I'm not sure if it is accessible. This would eliminate the need for a separate app.SelectedCells variable that I update with the callback function.
Unfortunately I am still not aware of a solution.
I had this same problem in App Designer. The solution was to set the Column Editable property to true.

Sign in to comment.

Answers (3)

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

Hi Andres! Thank you very much for the reply.
If I am understanding correctly, I believe your suggestion does work to remove the data from the table. This is very similar to the way I implemented the "remove files" button in my program. My button did remove the files successfully. The problem is that the UITableCellSelection callback function is not triggered when I deselect files in the table. So, once I deselect a file from the list, then press "remove files", a file is still removed even though I don't want it to be.
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
@nada abd: You haven't understood at all the topic of the question. Your reply is irrelevant.

Sign in to comment.

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

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Asked:

on 10 Aug 2018

Answered:

on 8 Aug 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!