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

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.

 Réponse acceptée

Adam Danz
Adam Danz le 2 Juil 2019
Modifié(e) : Adam Danz le 2 Juil 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 commentaires

Pedro Guevara
Pedro Guevara le 2 Juil 2019
Modifié(e) : Stephen23 le 3 Juil 2019
It works perfectly ... thank you very much.
Forgive me, but I have another doubt. Do you know how I can eliminate the NaN that are emerging in the table?
Adam Danz
Adam Danz le 3 Juil 2019
Modifié(e) : Adam Danz le 3 Juil 2019
How is your UItable different from the one in my example?
And have you changed any parts in my example?
Pedro Guevara
Pedro Guevara le 3 Juil 2019
Modifié(e) : Pedro Guevara le 3 Juil 2019
Look, the problem is this. What happens is that as I said yesterday, your code works great (clean the row of the uitable), but there is another problem, and that is for the purposes of my code the row cleaned must be reusable for the entry of others data, something that unfortunately I can not achieve applying your code, since apparently the variables are changing type (from char to cell) and these in turn transfer those properties to the uitabla, affecting it in a negative way. I suppose that if I managed to leave every element of the matrix of the "char" form, I would not have have to no be problems with the uitable, but I'm not sure.
I show you some images so that you are a little clearer about the matter. I also leave my .m and .fig files for you to execute:
I also leave my .m and .fig files for you to execute. The way to access the cleaning condition of the row is as follows.
1) In the field "number of elements" you can type any number, for example 3.
2) you click on the "generate table" button
3) you create a uitable with as many rows as you entered in "number of elements"
4) The condition can only be accessed when the values ​​entered in column 7 and 9 are different, or when the values ​​entered in columns 6 and 8 are different.
Thank you very much and I hope to have been clear with my problem.
Pedro Guevara, I've been away for a few days. Have you resolved your problem?
If you still need some help,please provide an example that reproduces your problem so I can run it and troubleshoot it.
When I run the code you provided in the zip file, I get this error:
Undefined function or variable 'axes1_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form1('axes1_CreateFcn',hObject,eventdata,guidata(hObject))
Hi. What a shame the delay. I have been a little busy. I still need help with the problem. :(
I find it strange that the files are causing you an error, since I have tried them on other machines and they work perfectly. Here I send you the files again.
The problem I have is the one that I mentioned above, look at the images and the comment and you will see what happens. In short, when re-entering new data to a row that had previously been deleted (thanks to your code) these new data do not behave like numerical data and I cannot operate them mathematically when executing the actions of my button
"Matrices Rigideces K's "
Thank you very much for your help.
Pedro, I'm getting errors when I run both form1.m and form2.m (see below). I'm assuming you're opening each GUI by merely running the m files (no inputs).
In any case, is this still your currently question, "Forgive me, but I have another doubt. Do you know how I can eliminate the NaN that are emerging in the table?" ?
If that's your question, I don't need to run your GUI. You just need to show me what your GUI UITable looks like when the variables are loaded.
form1()
Undefined function or variable 'axes1_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form1('axes1_CreateFcn',hObject,eventdata,guidata(hObject))
form2()
Undefined function or variable 'form2_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form2('form2_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'Can_port_label_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form2('Can_port_label_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'Aa_Label_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form2('Aa_Label_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'I_Label_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form2('I_Label_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'Av_Label_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form2('Av_Label_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'Fa_Label_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form2('Fa_Label_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'Fv_Label_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form2('Fv_Label_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'TablaMasa_CreateFcn'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in form2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)form2('TablaMasa_CreateFcn',hObject,eventdata,guidata(hObject))
Reference to non-existent field 'TablaDatosElementos'.
Error in form2>form1_OutputFcn (line 74)
set(handles.TablaDatosElementos,'Data',cell(1,9));
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in form2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Hi. Thank you very much for answering again. The problem I currently have, as I indicated in the last comment, is that I cannot reuse the deleted row to enter new numerical data without errors. I don't know why the mistakes you are having in the execution may be due. later I will leave a video with the way in which the application should be executed.
Thank you very much for your help.
What I need to see is
  1. the UI table before row deletion,
  2. the same UI table after row deletion,
  3. the data you're trying to put into the UI table that causes the error and
  4. the full copy-pasted error message.
Hello again. Look, what happens is that I can't show you an error message because the program skips the lines of code with the data re-entered in the deleted row (re-entered data that remain from the NaN Format). I illustrate the problem with these images so that you understand me better:
The second row of this table was deleted (since it does not meet the orthogonality condition of the portico you program), re-enter the data back to the deleted row (see how the distribution of the numbers entered with respect to those of the rows is 1 and 3, are different)
3.1.png
In this image you can see how it is when I click on my "Matrices rigides" button The variable "datos" recognizes the data re-entered in row 2 com of type NaN.
3.png
In the following image I can show you how the program (and how the handling of variables goes) enters the sentence that determines the orthogonality, lines before deleting the second row.
1.png
and in this last image you can see how the variable "aux_tab" looks after the second row was deleted.
2.png
I will soon upload the video I had promised to upload. Thank you very much for your help.
Here I leave the video the problem I have in my guide.
https://youtu.be/JNf8IlAxsnM
Pedro, we're getting closer and I think I understand what's going on.
The numeric data in your table are right-justified but there are strings in your table that are left-justified. Unless you've specified that the data are numeric, when you enter numeric data the UITable treats it as a string. That's why 0.005 becomes '0.005'.
I believe there are two options to fix this. Try option 1 first and if that doesn't work, try option 2.
Option 1: Specify the ColumnFormat as Numeric.
  • in GUIDE GUIs open the gui figure through guide(), right-click the UItable, select TablePropertyEditor, and then select "numeric" for all of the column formats on the right.
  • in app designer, in the startupFcnI(), specify the column formats (see code below)
  • for uitable() do-it-yourself designs, specify the column formats (see code below).
% For app designer
app.UITable.ColumnFormat = {'numeric'};
% For uitable() do-it-yourself designs
uitable(. . . ,'ColumnFormat',{'numeric'});
Option 2: Specify the ColumnFormat at Strings
Using the same ideas as option 1 above, you can set the format to strings and use str2double() which alread exists in your code to convert to numeric.
Hi. I put in your code the expression str2double (line 4 of the following code) and although I can reuse the "deleted" row to reintroduce new data (and put erased in quotes, because now what it does is replace the entire row with NaN) by not the orthogonality condition is met. I leave images.
aux_tab = get(handles.TablaDatosElementos,'Data');
rowIdx = [ CelDig(1,1) ];
aux_tab(rowIdx,:) = cell(numel(rowIdx),size(aux_tab,2));
tabladatos=set(handles.TablaDatosElementos,'Data',str2double(aux_tab));
1.png
2.png
Thanks for your help.
I'm not sure I understand.
Question 1: Are you saying that your GUI is working now except instead of automatically emptying a row, it fills it with NaNs?
Question 2: If the answer to Q1 is 'yes', is that really a problem? If the answer to Q1 is no, please explain the new problem again because I didn't follow.
P1. The answer is yes. But by deleting the NaN and setting numerical values ​​the program works fine.
P2. It is for aesthetics and not to have to delete every NaN in the row. XD
I see. NaNs do not pose a problem to your analysis. You just don't want to see them.
In that case you'll have to use option 2 in this comment (link below) and instead of replacing the empty row with [], you should replace it with ''.
You are going to want to kill me, but I am not sure about which line of your code I should make the change you manifest ( replace the empty row with [ ], you must replace it with ' ') . Sorry
aux_tab = get(handles.TablaDatosElementos,'Data');
rowIdx = [ CelDig(1,1) ];
aux_tab(rowIdx,:) = cell(numel(rowIdx),size(aux_tab,2));
tabladatos=set(handles.TablaDatosElementos,'Data',str2double(aux_tab));
Adam Danz
Adam Danz le 27 Juil 2019
Modifié(e) : Adam Danz le 27 Juil 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);
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);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by