Is there any way to "clean" a single row of a uitable?

2 views (last 30 days)
Good day. I have the following problem I have a uitable to which some data must be entered. What I want is to clean a row where you are entering the data whenever a condition is fulfilled. Currently I have a line of code that allows me to clean all the contents of the table, but as I said, I just need to clean the row that meets the condition. Here I owe part of the code for your evaluation:
function TablaDatosElementos_CellEditCallback(hObject, eventdata, handles)
if ( ( datos ( V_org(celdas,1),7 ) == datos ( V_org(celdas,1),9 ) ) || ( datos ( V_org(celdas,1),6 ) == datos ( V_org(celdas,1),8 ) ) ) % Condition NOT TO BE FULFILLED TO CLEAN THE ROW.
else
set(handles.TablaDatosElementos, 'Data', cell(size(get(handles.TablaDatosElementos,'Data')))); % THIS IS THE CODE LINE THAT CLEAN ME ALL THE UITABLE.
end
end
tabla.JPG
I hope you can help me with this problem, since after a lot of research it seems that there is no way to do a selective cleaning of the uitable. Thank you.

Accepted Answer

Adam Danz
Adam Danz on 2 Jul 2019
Edited: Adam Danz on 2 Jul 2019
To clear a list of rows from a table, copy the entire table into a local variable, empty the desired rows, and then reassign the local table back to the UI table.
% Create demo UItable
d = {'Male',52,9;'Male',40,0;'Female',25,9};
f = figure;
handles.TablaDatosElementos = uitable(f,'Data',d,'Position',[20 20 262 204]);
% 1) copy current table to a local variable
t = get(handles.TablaDatosElementos,'Data');
% 2) Identify which row numbers to clear
rowIdx = [1,2]; % Clear rows 1 and 2
% 3) Clear those rows in the local variable
t(rowIdx,:) = cell(numel(rowIdx),size(t,2));
% 4) reassign updated table to the GUI
set(handles.TablaDatosElementos,'Data',t);
  19 Comments
Adam Danz
Adam Danz on 27 Jul 2019
Edited: Adam Danz on 27 Jul 2019
Pedro, here is the ENTIRE workflow. This is an independent, working example of every step from setting up the UI table, loading it with data, copying the data into a variable, removing a row, and updating the data in the UI table.
Go through every single line, one-by-one to understand what's going on and pay attention to the comments.
Before you apply this to your existing code, save a copy of your code because it works (remember, you're just unhappy with the NaNs which really don't cause any problem with the analysis).
% Create a matrix of NUMBERS
d = randi(10,3,3);
% Convert matrix of NUMBERS into cell array of CHARS
ds = compose('%d',d); %Requires r2016b
%ds = sprintfc('%d', d); % undocumented alternative
% Create a demp UITable
f = figure;
h = uitable('Position',[20 20 262 204]);
h.Data = ds; % IMPORT THE CELL ARRAY OF STRINGS AS DATA
% Copy current table to a local variable
t = get(h,'Data'); % <---- still a cell array of strings
% Identify which row numbers to clear
rowIdx = [1,2]; % Clear rows 1 and 2
% Clear those rows in the local variable
t(rowIdx,:) = {''};
% Reassign updated table to the GUI
set(h,'Data',t);
% Get a NEW copy of the current table
t2 = get(h,'Data'); % <---- still a cell array of strings
% Assign new data in the first row
newdata = [1,2,3]; % NUMERIC!
ds2 = compose('%d',newdata); % CELL OF STRINGS
t2(1,:) = ds2;
set(h,'Data',t2);
Pedro Guevara
Pedro Guevara on 30 Jul 2019
Thank you. You're very kind. There were some problems when modifying 2 rows in a row (the rows deleted after the first one were marked with "NaN"), but I managed to solve it with a code that I found in the forum, or at least I think so :). I leave the code here so it can help anyone who needs it. Thank you very much for your huge help. Maybe it bothers you again in the future.
d = datos;
% Convert matrix of NUMBERS into cell array of CHARS
ds = compose('%d',d); %Requires r2016b
%ds = sprintfc('%d', d); % undocumented alternative
% Create a demp UITable
f = figure;
h = uitable ('Position',[20 20 262 204]);
h.Data = ds; % IMPORT THE CELL ARRAY OF STRINGS AS DATA
% Copy current table to a local variable
t = get(h,'Data'); % <---- still a cell array of strings
% Identify which row numbers to clear
rowIdx = CelDig(1,1) ; % Clear rows 1 and 2
% Clear those rows in the local variable
t(rowIdx,:) = {''};
% Reassign updated table to the GUI
set(h,'Data',t);
% Get a NEW copy of the current table
t2 = get(h,'Data'); % <---- still a cell array of strings
t2 = strrep (t2, 'NaN' , '' );
set(handles.TablaDatosElementos, 'Data',t2);

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!