comparing table values using isequal
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (3)
Fangjun Jiang
il 15 Set 2020
Modificato: Fangjun Jiang
il 15 Set 2020
1 voto
try TF = isequal (M.Var2{i},'f')
1 Commento
Jakub Steiner
il 15 Set 2020
BOB MATHEW SYJI
il 15 Set 2020
0 voti
Instead of
TF = isequal ("M(i,2)",'f')
try,
TF = strcmp (M.2nd_variable,'f')
The other problem with table equality that is worth mentioning - !! (this is a mistake, see comment below) 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. (!! also a mistake) 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.
2 Commenti
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.
Steven, Thanks. That's useful to know.
Having done some experiments, it seems I got it wrong - actually isequal(T1, T2) does not compare the non-significant Properties of a table. My mistake was that I was comparing data from T1 = readtable('xxx.csv') with T2 = readtable('xxx.xlsx') and the numbers were coming in different by 1*eps(...) which wasn't visible even with format('long').
I personally solved the problem by
T1 = varfun(@single, T1);
T2 = varfun(@single, T2);
(T_diff, T_i) = setdiff(T1, T2);
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!