eval
evaluates a valid Matlab expression which is defined by a string.
Imagine the following:
name = 'myVar';
nominal = 42;
when you now call:
eval([ name '=' num2str(nominal) ';' ]);
which is the same like:
eval([ 'myVar = 42;' ]);
you get a variable myVar
in your workspace which has the value 42.
The same happens, when you type in
myVar = 42;
directly. So instead of having a line of code in your script, you can just evaluate a code-string from wherever. In your case it is used to create a variable from two struct fields, the first the variable name and the second it's value.
Another example, you want a variable named after it's actual value. But you don't know its name before:
value = randi(10);
eval([ 'var' num2str(value) '=' num2str(value) ';' ]);
The only way to do this is by using eval
as you need to create a code-line-string according to the random generated value. Eval then "writes" it. If for example value = 9
, it is stored to a variable var9
in the next step.