Matlab: using string as condition for if statement

cond = 'a==1';
a=1;
if (cond)
b=0;
end

Hello! Is there a way to do what I wrote above? In a text variable, I need to write a condition (even complex, using && and || too), and then simply insert the variable in the IF statement. I tried this example, but unfortunately it did not work. Can you solve it?

Edit: more information for you!
I repeat the various trading strategies for the project.
Inside the common M file, I use a function for each strategy that I need for testing. Each strategy receives input about the current situation, and then the function evaluates the behavior of the trading strategy in accordance with the data (as well as in accordance with the requirements of margin and other materials that are not dependent on the strategy). The only thing that differs in each function is the entry or exit rule. Each strategy has certain entry and exit conditions (for example, "open a long position when ... and ..." or "close a short position when ... or ...").
In the main M file, I use a loop to simulate the passing time, but I want to implement an additional external loop, which is the amount of strategy tested. In addition, until now, each condition in IF statements is written manually, and I would like to get a unique function (no more than 1 for each strategy, as it is now), which, in accordance with the index of the strategy loop, would pass the input and output of the conditions taken from matrix of rows.

Hope this will be possible.

+4
source share
1 answer

I think this is what you want:

cond = 'a==1';
a=1;
if eval(cond)
  b=0;
end

However, it evalis evil. Try not to use it if possible: http://blogs.mathworks.com/loren/2005/12/28/evading-eval/

+7
source

All Articles