How to compare two input arguments of type table

I am trying to import my data from .csv file. This data includes characters and dates(with time). I imported this data as a table and now try to perform simple operations such as comparing the character field with a value or compare two date field. I used the following:
formatSpec = '%f%C%C%C%{MM/dd/yyyy HH:mm}D%{MM/dd/yyyy HH:mm}D';
T = readtable('Flights.csv','Delimiter',',', ... 'Format',formatSpec);
T
and the output looks like this:
T =
FLIGHTID ACID DEPTAIRPORT ARRAIRPORT DEPTDATE ARRDATE
______________ _______ ___________ __________ ________________ ________________
20160100000000 DAL1448 ATL DTW 01/06/0016 12:10 01/06/0016 17:10
Now I need to check to see if DEPTDATE is earlier than ARRDATE or later. I used the following syntax and got an error:
if T(1,5) > T(1,6) 1 else 2 end
error: Undefined operator '>' for input arguments of type 'table'.
Error in input (line 12) if T(1,5) > T(1,6)
Also, how can I check to see if T(1,2) = 'DAL1448' and if yes, then print "match found"?
Thanks for your help.

 Réponse acceptée

michio
michio le 22 Sep 2016
Modifié(e) : michio le 22 Sep 2016
If you want to compare two single values of the same type in the table variable, use {} instead of (),
T{1,5} > T{1,6}
Also
idx = T{:,5} > T{:,6};
T(ind,:)
could find the rows that match the condition (DEPTDATE is earlier than ARRDATE or later). To find rows with ACID being 'DAL1448',
T(T.ACID == 'DAL1448',:)
may do the job.
varfun is also a convenient function that applies function to table variables.

4 commentaires

Ehsan Es
Ehsan Es le 22 Sep 2016
Michio, Thanks for your help. It did it.
More readable, but equivalent, syntaxes might be
T.DEPTDATE(1) > T.ARRDATE(1)
or
T{1,'DEPTDATE'} > T{1,'ARRDATE'}
Hope this helps.
ADC
ADC le 15 Jan 2019
I've got the same problem, I've imported data from a datasheet and I need to filter only the row that contain an assigned value, but it does'nt work!!!
the error I get is:
Undefined operator '==' for input arguments of type 'cell'.
I tryed to use this exspression:
A(A.NAerogenerador == '109',:)
or this
W=A(A(:,2)==109,:);
but still does'nt work
Presumably A.NAerogenerador is a cell array of text, in which case you need strcmp. Switch to using string arrays, and you can use ==.
A(:,2)==109 won't work for two reasons:
1) A(:,2) is a (one-variable) table, which == won't work on
2) A{:,2}==109 would be the second variable (probably equivalent to A(:,2)==109, but hwo knows with no example), and == doesn't work on cell arrays, and in any case 109 is numeric.

Connectez-vous pour commenter.

Plus de réponses (1)

Hi,
I've got the same issue, before upload Matlab it worked but now it crases. It's not my code and I don't understand a lot so I don't really know where is the problem.
for n=1:size(correct,1)
if correct(n,1)>0 || correct(n,2)>0
peaks=polyval(parms(n,:),ex)
end
end
But I receive an error message:
"Undefined operator '>' for input arguments of type 'table'.
if correct(n,1) > 0 || correct(n,2) > 0 %test for no-filter condition"
I tried to change parentheses by curly braces after some research but unsuccessfully...it leads to other errors...

1 commentaire

Lucie, you should start a new thread.
It appears that "correct" used to be a numeric matrix, and now it's a table. That's happening upstream of the code you are looking at, you you need to provide more context.
In a new thread.

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by