comparing table values using isequal
Mostrar comentarios más antiguos
Hi,
I have a table M. The second column contains either the letter 's' or letter 'f'.
I would like to put all the rows with the letter 'f' in the second column into a new table A with the following code
pocet_radku = height(M);
j=1;
for i=1:pocet_radku
TF = isequal ("M(i,2)",'f')
if TF == 1
A(j,:)= M(i,:);
j = j+1;
else
end
end
Unfortunatelly, the isequal function return only zeros. Any idea why? In there syntax error that I'm not aware of?
Testing in a command window:
>> isequal ("M(180,2)",'f')
ans =
logical
0
>> M(180,2)
ans =
table
Var2
_____
{'f'}
Thanks a lot for any answers
Respuesta aceptada
Más respuestas (3)
Fangjun Jiang
el 15 de Sept. de 2020
Editada: Fangjun Jiang
el 15 de Sept. de 2020
1 voto
try TF = isequal (M.Var2{i},'f')
1 comentario
Jakub Steiner
el 15 de Sept. de 2020
The other problem with table equality that is worth mentioning - if you use setdiff() or other methods that work on equality, the comparison is made including the table properties, not just the data in the table. Most of the time this doesn't matter, but if you get your tables out of data files using readtable(), it's easy to end up with "useful" extra data in the table properties that makes comparison difficult.
In my case, I was comparing two data sets, one of which was stored in .csv format, the other in .xlsx format. The column headers in the spreadsheets were sometimes valid matlab variable names and sometimes not (eg. mass[1], mass[2]...). The variable names were autoconverted to mass_1_, mass_2_ and in the case of the .csv file import, the orginial name was stored in mytable.Properties.VariableDescriptions, but only for autoconverted column names. As a result, selecting columns from the table and comparing them automatically returned false if the table property had a VariableDescriptons property populated, even if the data content is identical.
1 comentario
As of a couple releases ago, you can have table arrays with row and variable names that aren't valid MATLAB variable names (as validated by the isvarname function.) They can now have spaces, square brackets, etc.
A = array2table(magic(4), VariableNames = ["this name includes spaces", "mass[1]", "12345", "x"])
Note that if you wanted to retrieve those variable names that aren't isvarname using A.variablename syntax, you'll have to handle the variablename specification a little specially.
A.("mass[1]")
A.x % This works fine though
But you can use those names with normal () or {} indexing.
A(3, "mass[1]")
The functions for importing tabular data into tables or timetables should have a name-value argument that lets you specify if you want to preserve the variable names or to modify them as in earlier releases. For readtable that argument is named VariableNamingRule.
BOB MATHEW SYJI
el 15 de Sept. de 2020
0 votos
Instead of
TF = isequal ("M(i,2)",'f')
try,
TF = strcmp (M.2nd_variable,'f')
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!