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);

 채택된 답변

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by