# Determine the relational operator in an expression

2 views (last 30 days)

Show older comments

I need to find out the operator in an expression.

I tried to convert the symbolic expression using char and then find the position of the operator ("<=","==",">="). However, when using the char function, Matlab always puts it in the form "<=" when it is an inequality. Therefore I never find the gt operator.

syms x y

exp1 = x <= y;

exp2 = x == y;

exp3 = x >= y;

charExp = char([exp1,exp2,exp3])

##### 2 Comments

John D'Errico
on 15 Nov 2023

syms x y

exp1 = x <= y

exp2 = x == y

exp3 = x >= y

But it also flipped the left and right hand sides. So the two are equivalent. All that matters is what is on each side.

Torsten
on 15 Nov 2023

Your expressions already have to be of type "char", not of type "sym", to extract the operator used.

### Answers (3)

Pooja Kumari
on 15 Nov 2023

Edited: Pooja Kumari
on 15 Nov 2023

Dear Geovane,

It is my understanding that you are facing issues with converting symbolic expression using "char".

It seems that the "char" function in MATLAB always represents inequalities using the "<=" operator, regardless of the original operator. Unfortunately, there is no direct way to obtain the original ">=" operator using the "char" function. It is clear that "exp3" will give "x>=y" but it switched to "<=" which is correct expression as shown below:

syms x y

exp3 = x >= y;

char(exp3)

Regards,

Pooja

##### 0 Comments

Image Analyst
on 15 Nov 2023

Then you can use contains to run down the various math operators you might possibly encounter, and then take appropriate action

syms x y

exp3 = x >= y;

% Cast to string.

thisExpression = char(exp3)

if contains(thisExpression, '>=')

% Operator is >= so do something with that knowledge.

fprintf('Operator is >=.\n');

elseif contains(thisExpression, '>')

% Operator is > so do something with that knowledge.

fprintf('Operator is >.\n');

elseif contains(thisExpression, '<=')

% Operator is <= so do something with that knowledge.

fprintf('Operator is <=.\n');

elseif contains(thisExpression, '<')

% Operator is > so do something with that knowledge.

fprintf('Operator is <.\n');

elseif contains(thisExpression, '~=')

% Operator is ~= so do something with that knowledge.

fprintf('Operator is ~=.\n');

% etc for the other possible operators.

end

##### 7 Comments

Image Analyst
on 16 Nov 2023

Edited: Image Analyst
on 16 Nov 2023

Les Beckham
on 16 Nov 2023

If you can rely on x always being on the left in your "test" expressions, you can test for y being on the left in the result returned by char (to detect that the symbolic toolbox reversed the order of the comparison). For example:

syms x y

exp1 = x <= y;

exp2 = x == y;

exp3 = x >= y;

charExp3 = char(exp3)

if contains(charExp3, '<=')

if charExp3(1) == 'x'

operator = '<='

else

operator = '>=' % if symbolic toolbox swapped the comparison, swap the result

end

end

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!