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

Cris LaPierre
Cris LaPierre el 15 de Sept. de 2020
Editada: Cris LaPierre el 15 de Sept. de 2020
This can be done much simpler. Try something like this.
A = M(M{:,2}=="f",:);

2 comentarios

Jakub Steiner
Jakub Steiner el 15 de Sept. de 2020
Nice! Works as well and without my clumsy cycles.
Thanks a lot!
This can be made even simpler by using the variable name:
A = M(M.Var2=="f",:);

Iniciar sesión para comentar.

Más respuestas (3)

Fangjun Jiang
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')
Alvery
Alvery hace alrededor de 5 horas
Editada: Alvery hace alrededor de 5 horas

1 voto

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"])
A = 4×4 table
this name includes spaces mass[1] 12345 x _________________________ _______ _____ __ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
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]")
ans = 4×1
2 11 7 14
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A.x % This works fine though
ans = 4×1
13 8 12 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
But you can use those names with normal () or {} indexing.
A(3, "mass[1]")
ans = table
mass[1] _______ 7
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.

Iniciar sesión para comentar.

BOB MATHEW SYJI
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

Productos

Versión

R2020a

Etiquetas

Preguntada:

el 15 de Sept. de 2020

Comentada:

hace alrededor de 5 horas

Community Treasure Hunt

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

Start Hunting!

Translated by