This is a thought example of what I am thinking of:
test = 'x > 0';
while str2func(test)
Do your thing
x=x-1;
end
Is it possible to store whole logical operations in a variable like this?
Of course the str2func will break here. If it is possible this function will likely be something else. And I have only added apostrophes to the test variable content, because I cannot think of what else would be the storing method.
I can see it usefull when sending arguments to functions and alike. But mostly I’m just wondering, because I have never seen it done in any programming language before.
You can store the textual representation of a function in a variable and evaluate it, for example
should result in 1 or 0 depending on x’s value.
But you shouldn’t use
evalfor reasons too-often covered here on SO for me to bother repeating. You should instead become familiar with functions and function handles. For examplemakes
testa handle to a function which tests whether its argument is greater than 0 or not.Many languages which are interpreted at run-time, as opposed to compiled languages, have similar capabilities.