Weird behaviour in MATLAB regarding eval()

1

Consider the following code

x = zeros(3,3);
eval(str3)
g = @(x) eval(str3)
g(x)
g(zeros(3,3))

MATLAB will evaluate eval(str3) as intended, but complain about the other two claiming they are Undefined function for input arguments of type 'double'. Why?

Markus Klyver

Posted 2017-03-25T03:07:03.513

Reputation: 111

Answers

0

I can understand why Matlab throws an error. str3 is undefined in your example but from your description, I can make that str3 was defined in your workspace and contained a string. At line 3, you made a function that is independent of x that evaluates whatever str3 was before you executed line 3, every time g is called. I think your line 3 does not reflect what you intended.

I think what you try to do is something along the lines:

    g = @(x) eval(x);
    g('x = zeros(3,3)')

But you haven't described what you aimed to achieve. Please provide more details.

Sander

Posted 2017-03-25T03:07:03.513

Reputation: 156