comparing table values using isequal

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

 采纳的回答

This can be done much simpler. Try something like this.
A = M(M{:,2}=="f",:);

2 个评论

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",:);

请先登录,再进行评论。

更多回答(3 个)

Fangjun Jiang
Fangjun Jiang 2020-9-15
编辑:Fangjun Jiang 2020-9-15
try TF = isequal (M.Var2{i},'f')
Instead of
TF = isequal ("M(i,2)",'f')
try,
TF = strcmp (M.2nd_variable,'f')
Alvery
Alvery 2026-8-6,18:01
编辑:Alvery 2026-8-7,17:14
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 个评论

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.
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);

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

版本

R2020a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by