MATLAB: Using operators (+,-,*,/) in variables

MATLABoperators

Hi, if i need to config for + to be 'op' for example how can i then use it as an actual + further down the code?
example
op='+';
a=1;
b=2;
c=a(+ as op)b;

Best Answer

Use str2fun to change your string into a function handle. Note that the string content must be a valid matlab function name ( '+' is)
op = '+';
a = 1;
b = 2;
opfn = str2fun(op);
c = opfn(a, b);
There is no way to have it
c = a opfn b; %can't be done in matlab