UITable Individual Cell Editing in App Designer

52 visualizaciones (últimos 30 días)
Leah O'Shea
Leah O'Shea el 3 de Ag. de 2022
Respondida: Voss el 14 de Dic. de 2023
Hello All,
I wanted to put my code out there to hopefully help others in my situation that I could not find a specific answer for. I have created an app that inputs data into a UITable. The user inputs data in the "Collection Data" section and then adds it to the table as a new row with the "Add" button. Each time this is done the table is updated:
% Value changed function: ADDButton
function ADDButtonValueChanged(app, event)
app.UITable.Data = app.T; %T is a private property
PNP = app.PNPEditField.Value;
Minute = app.MinuteEdit
Field.Value;
Trial = app.TrialEditField.Value;
EF = app.EFEditField.Value;
Ash = app.AshEditField.Value;
Tar = app.TarEditField.Value;
aMin = app.aMinEditField.Value;
aMax = app.aMaxEditField.Value;
R1 = app.R1EditField.Value;
Timer = app.TimerEditField.Value;
nr = {PNP Minute Trial EF Ash Tar aMin aMax R1 Timer};
app.UITable.Data = [app.T; nr]; %new row added
app.T = app.UITable.Data;
end
This would work perfectly until I accidently added a new row with incorrect information. I made the cells of the table to be editable but when a value was changed and a new row was added the cell would go back to the original value. I also got the error that Matlab was “Unable to perform assignment because the size of the left side is x-by-x and the size of the right side is x-by-x.” if I change the value to be longer or if I wanted to delete the data that I accidently input. Therefore, I created the callback function UITableCellEdit:
% Cell edit callback: UITable
function UITableCellEdit(app, event)
selectedCell = event.Indices; %cell selected
newData = event.NewData; %data entered
if (newData > 0)
app.T(selectedCell(1,1),selectedCell(1,2)) = cellstr(newData); %if data is changed with different value
else
app.T(selectedCell(1,1),selectedCell(1,2)) = cellstr(''); %if data is to be deleted
end
end
I had to convert the newData to be a string because otherwise I would get the error “Conversion to cell from char is not possible” and to delete the data I just made it a blank string because I would get the error “A null assignment can have only one non-colon index” when trying to assign the cell to [ ].
A lot of the posts I was reading was for deleting full rows or columns but I wanted to change a specific cell which this code now allows me to do.
I hope this helps!
  2 comentarios
FEH
FEH el 13 de Abr. de 2023
Hi Leah
Can I know what is app.T in your coding? I face some difficulty to add new row after input data in Edit Field.
Leah O'Shea
Leah O'Shea el 13 de Abr. de 2023
In the beginning of my code I create a private property in properties (Access = private) so that I can a assign the table to that variable and allow it to be called in other functions as app.T.
properties (Access = private)
T % Table for collection
t % table for baseline values
end

Iniciar sesión para comentar.

Respuestas (1)

Voss
Voss el 14 de Dic. de 2023
"when a value was changed and a new row was added the cell would go back to the original value"
Because ADDButtonValueChanged doesn't add a new row to the uitable's existing Data; it adds a new row to app.T and sets that as the uitable's Data.
function ADDButtonValueChanged(app, event)
% (this sets the table's Data to app.T and is unnecessary:)
app.UITable.Data = app.T;
% ...
% get the variables from the editfields
% ...
nr = {PNP Minute Trial EF Ash Tar aMin aMax R1 Timer};
% this sets the uitable's Data to app.T followed by the new row:
app.UITable.Data = [app.T; nr];
% update app.T:
app.T = app.UITable.Data;
end
To keep the existing uitable Data and add a new row, you should do:
function ADDButtonValueChanged(app, event)
% ...
% get the variables from the editfields
% ...
nr = {PNP Minute Trial EF Ash Tar aMin aMax R1 Timer};
% this sets the uitable's Data to the existing Data followed by the new row:
app.UITable.Data = [app.UITable.Data; nr];
% (alternate expression to do the same thing:)
% app.UITable.Data(end+1,:) = nr;
% update app.T:
app.T = app.UITable.Data;
end
"I had to convert the newData to be a string because otherwise I would get the error “Conversion to cell from char is not possible” and to delete the data I just made it a blank string because I would get the error “A null assignment can have only one non-colon index” when trying to assign the cell to [ ]."
Both of those errors can be avoided by using curly braces {} to access the contents of the cell:
function UITableCellEdit(app, event)
selectedCell = event.Indices; %cell selected
newData = event.NewData; %data entered
if newData > 0
app.T{selectedCell(1,1),selectedCell(1,2)} = newData; %if data is changed with different value
else
app.T{selectedCell(1,1),selectedCell(1,2)} = []; %if data is to be deleted
end
end
Notice that that's updating app.T and not updating app.UITable.Data (so the uitable doesn't change as a result of executing this cell edit callback). You can have the function apply to the uitable by replacing app.T with app.UITable.Data in that function.
In fact, if app.T is intended to be a copy of whatever Data is in the uitable at any time, then you can remove property T from the app entirely and just use app.UITable.Data throughout to get the uitable's data whenever you need it.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by