comparison between a string/or cell and categorical data

hi,i ve this problem
Trading=categorical({'As is';'bla'})
B='As is';
class(B)='char'
b==Trading(1)
> 0
how can i compare a value of type char and a categorical data type?
(

Answers (2)

MATLAB is correct.
Sis(1).Trading = 'As Is'
Sis = struct with fields:
Trading: 'As Is'
Trading(1) = categorical({'As is'})
Trading = categorical
As is
Sis(1).Trading==Trading(1)
ans = logical
0
If you used a capital I in the definiition of Trading or a lower-case i in the definition of Sis(1).Trading you'd receive the results you expected.
SisLC.Trading = 'As is';
SisLC.Trading == Trading(1)
ans = logical
1
TradingUC = categorical({'As Is'});
Sis(1).Trading == TradingUC
ans = logical
1

1 Comment

I realized now that it'is "Is" and not "is" .. sorry

Sign in to comment.

Your method works for me:
Trading = categorical({'As is';'bla'});
B='As is';
B == Trading(1)
ans = logical
1
You did have a lower-case "b" in your code. MATLAB is case-sensitive. Maybe that was part of the problem?

5 Comments

I made a transcription error but on my example it doesn't work
class(Sis(i).Trading)
ans =
'char'
Sis(i).Trading
ans =
'As Is'
Trading(1)
ans=
categorical
As is
Sis(i).Trading==Trading(1)
ans =
logical
0
strcmp(Sis(i).Trading,Trading(1))
ans =
logical
0
i reported my example with simple variables but using my own variables it doesn't work
(i used both == and strcmp)
Can you upload the variables in a MAT file? I think that any way I choose to define the above, it is going to work as in your simple example.
another example .
>> Trading(1)
ans =
categorical
As is
>> v
v =
'As Is'
>> Trading(1)==v
ans =
logical
0
We don't need another example. Please upload your data. You can use the paper clip icon in the INSERT section of the toolbar.
As I said above, I can define the variables myself and get your example to work. The key to understanding your problem is to use the data you are actually working with.
Trading(1) = categorical({'As Is'});
v = 'As Is';
Trading(1) == v
ans = logical
1
>> test
ans =
categorical
As is
ans =
logical
0

Sign in to comment.

Categories

Asked:

on 4 Jun 2023

Edited:

on 4 Jun 2023

Community Treasure Hunt

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

Start Hunting!