Is it Possible to store an if Condition in a String to manipulate later ?

1 visualización (últimos 30 días)
Am I able to do something like this in Matlab? Please advice
aaa = 'r > 0 && r < 5 '; if (aaa) r = r+1; end

Respuesta aceptada

Orion
Orion el 22 de Oct. de 2014
Editada: Orion el 22 de Oct. de 2014
you can do that using eval
r=1;
aaa = 'r > 0 && r < 5 ';
if eval(aaa)
r = r+1;
end
disp(r);

Más respuestas (1)

Guillaume
Guillaume el 22 de Oct. de 2014
Editada: Guillaume el 22 de Oct. de 2014
If you really want to store your condition in a string, then use eval as Orion said. However, I wouldn't do that as you don't get an syntax check for the code in your string.
Rather, I'd use an anonymous function:
aaa = @(x) x>0 && x<5; %anonymous function, the name of the variable doesn't matter
if aaa(r) r=r+1;

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by