> I have a code that writes a string depending on what happened before in the program. The string created is more MATLAB code possibly containing for loops or if clauses.
I think you should seriously consider solving your problem in a different way. Creating code on they fly is usually not a good idea.
> I want to execute the string as if it were MATLAB code, i tried to use the eval() function but it did not work out. > > A simple example is: > > a=5; > eval('if a>4 b=1 end'); > > MATLAB returns the error: > > Error: Illegal use of reserved keyword "end".
Which is exactly what happens if the string were MATLAB code outside of eval, so it did just what you said you wanted:
>> a=5; >> if a>4 b=1 end if a>4 b=1 end | Error: Illegal use of reserved keyword "end".
Maybe you actually wanted to do something like this instead?
>> if a>4, b=1, end
But again, try not to use eval. Even if you ignore the debugging/maintaining problems with it, eval will very likely be slower than other routes to your original problem.