Clear Filters
Clear Filters

Operator '==' is not supported for operands of type 'table'.

34 views (last 30 days)
I am tyring to extract each ID for the table attached in the image (table is really large so did not attach the whole table). My code is as follows but I continue to get this error "Operator '==' is not supported for operands of type 'table'." when
A=[c];
R=c(:,"SAPID")
T=transpose(1:127)
u=(unique(R))
TurbineTest1=A(A.SAPID==u(1,1),:);
TurbineTest2=A(A.SAPID==u(2,1),:);
TurbineTest3=A(A.SAPID==u(3,1),:);
TurbineTest4=A(A.SAPID==u(4,1),:);
TurbineTest5=A(A.SAPID==u(5,1),:);
TurbineTest6=A(A.SAPID==u(6,1),:);
TurbineTest7=A(A.SAPID==u(7,1),:);

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 6 Aug 2021
Try this:
TurbineTest1=A(strcmp(A.SAPID,u(1,1)),:);

More Answers (1)

Peter Perkins
Peter Perkins on 6 Aug 2021
There are a couple of problems. c is a table, so
R = c(:,"SAPID")
is also a table, with only one variable. A table is a container, so
u = unique(R)
isn't going to work, and even if it did,
A.SAPID == u(1,1)
wouldn't. You want
u = unique(A.SAPID)
Then unique works, and == works. But wait, A.SAPID is a cell array of char, so == is still not going to work. As Scott suggests, you can use strcp. But I'd recommend using a string array for text data, not cell. It might be as simple as using "TextType","string" when you import data.
BUT ACTUALLY, values in SAPID appear to be drawn from a finite set of choices, in other words, they are categorical data. You probably should be using a categorical array.
A.SAPID = categorical(A.SAPID);
Then, all you need to do is iterate over categories(A.SAPID). == just works.
u = categories(A.SAPID);
TurbineTest1 = A(A.SAPID==u(1),:);
BUT HANG ON. Do you really want maybe dozens of separate tables in your workspace? I strongly recommend that you look at things like groupsummary and rowfun to see if you can do operations on groups in your data without pulling it apart.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!