Convert String to Expression for IF Statement

Issue: conditional expression for IF-statement is dynamic
if cond
a = 1;
else
a = 2;
end
"cond" will change according to input strings/values at each run. It can have any logic combination (<, >, =, &&, ||) and any length. Some examples:
  • "data1 > 10"
  • "data1 > 10 && data1 < 1000"
  • "data1 > 215 || data1 < -10"
  • "data1 > 35 && data2 = 2 && data 3 < 1000”
Conditions expressions will be read in as strings. Is there a way to convert the string to an executable expression?
Example: Running an equivalent version of the code
data = 10;
cond = 'data > 30';
if cond
a = 1;
else
a = 2;
end
returns a = 2.

 Respuesta aceptada

Rik
Rik el 19 de Jul. de 2019
data<1000
That will return a logical (true or false value).
parseCond=@(data) data<1000;
That will create an anonymous function that returns a logical. You can expand it easily like this:
parseCond=@(data) false;
parseCond=@(data) parseCond(data) || data<1000;
val=800;
parseCond=@(data) parseCond(data) && data>val;

1 comentario

Hi, Rik,
I wasn't very familiar with anonymous function. Thank you for the clarification. I tried the method and it works.
Since the cond in my case will be string, so I introduced a small change
Original string
'data > 30'
can be easily changed to
'@(data) data > 30'
so the code is
cond = '@(data) data > 30';
parsecond = str2func(cond);
data = 10;
if parsecond(data)
a = 1;
else
a = 2;
end

Iniciar sesión para comentar.

Más respuestas (1)

Christopher Wallace
Christopher Wallace el 19 de Jul. de 2019

0 votos

6 comentarios

Rik
Rik el 19 de Jul. de 2019
Better idea: change your code structure. Make cond either a logical or an anonymous function that returns a logical. That second one is likely what you prefer.
Eval is a bad habit in almost every circumstance.
YC
YC el 19 de Jul. de 2019
Hi, Rik, can you be more specific on how to make cond a logical or an anonymous function that returns a logical?
YC
YC el 19 de Jul. de 2019
Editada: YC el 19 de Jul. de 2019
I tried the code
data = 10;
cond = 'data > 30';
if eval(cond) == 1
a = 1;
else
a = 2;
end
and it gives desired output.
=========================
Update:
Since "eval" has many limitations, a different version using anonymous function is tested and works great.
cond = '@(data) data > 30';
parsecond = str2func(cond);
data = 10;
if parsecond(data)
a = 1;
else
a = 2;
end
For details, please refer to
Rik
Rik el 19 de Jul. de 2019
You don't need the ==1
Also, using eval is a bad idea. Click here.
Hi Rik,
I could've guessed the eval hate was coming :)
Can you explain why eval is a poor choice in this particular scenario. While I understand the security downfalls, especially if the condition being run is an input from a user, in this scenario it seems quite fitting. The function exists for a reason so throwing out the blanket statement "using eval is a bad idea" must be false is some instances.
Rik
Rik el 19 de Jul. de 2019
The reasons for the function existing are also discussed in the thread I linked. I don't think it would benefit the discussion to repeat those here.
In this scenario I would argue for a change on the input side that would prevent this strange setup. This data structure doesn't allow changing the variable name, while an anonymous function would (unless it's used with str2func, which is only marginally better than eval). It makes more sense to have the source return the actual test instead of a char array.
It isn't the security concerns for me. Personally I have a strong objection because you take away the automatic code checking tools, and tracking of variable names. And also that an error will not occur at the source.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

YC
el 19 de Jul. de 2019

Comentada:

Rik
el 19 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by