Why is the EVAL command not able to execute when I pass a java string with variables in it?

Why is the EVAL command not able to execute when I pass a java string with variables in it?
The following code does not work properly
clear foo
jstr=java.lang.String('foo=3;'); % create java string
eval(jstr) % evaluate foo in this (caller) workspace
who foo
The following code generates an error
foo=3;
jstr=java.lang.String('foo*2');
bar=eval(jstr); % BUG!! foo exists but is not found
??? Undefined function or variable 'foo'.
Error in ==> D:\MATLAB6p1\toolbox\matlab\lang\@opaque\eval.m
On line 18 ==> varargout{1:nargout} = eval(tryVal);

 Accepted Answer

This is a bug in MATLAB when using the EVAL function with a java string with variables. Our development staff is currently investigating this issue.
As a workaround, you can convert the Java String to a MATLAB Character array before calling the EVAL function. For example:
clear foo
jstr=java.lang.String('foo=3;');
eval(char(jstr))
who foo

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!