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.
What eval function does in this matlab code? - Stack Overflow
what is the function of 'eval' ?
What does the "eval" function do?
Dynamic function call using eval
Videos
Sorry if this is a trivial question - I never had any formal course in matlab and had to figure it out by myself. Recently I came across a piece of code that contains the "eval" function, so naturally I looked through the documentation, which badically said 'it's a function that evaluates things' and gave no examples.
Now I inow this might be something trivial that I should be aware of, but unfortunately I don't really know where to ask this question except here.
Any help would be appreciated.