如何从 MATLAB 函数内的工作区获取 Simulink 结构的值?
How do I get the value of a Simulink struct from the workspace within a MATLAB function?
我需要访问类型为 Simulink.parameter
:
的 MATLAB 工作区中变量的值
CAL_vars = dsdd('find','/path/CAL','ObjectKind','Variable','Property',{'name' 'Class' 'value' 'CAL'})
%gets ids of variables in data dictionary
i = 10
for i=1:length(CAL_vars)
var_name = dsdd('GetAttribute',CAL_vars(i),'name');
% gets names of variables in data dict
var_eval = eval(var_name); % this works in standalone script and it does exactly
% what I need, but once i put it in the function I need this for, it returns error
if (length(var_eval.Value) ==1)
if (var_eval.Value == true)
var_eval.Value = 1;
elseif (var_eval.Value == false)
var_eval.Value = 0;
else
end
end
% do something with the Value
if (errorCode ~= 0)
fprintf('\nSomething is wrong at %s\n', var_name)
end
end
出现问题是因为这些结构由 Simulink 制作并给出错误,当我尝试调用 eval(name_of_var): Undefined function 'eval' for input arguments of type 'Simulink.Parameter'.
奇怪的是,它似乎在独立脚本中正常运行,但一旦我将其插入更大的函数,它就会停止工作并开始显示错误
Error using eval
Undefined function or variable 'name_of_var'.
功能在工作区中一目了然。
Curiously, it seems to function properly in a stand-alone script but
once I plug it into the larger function, it stops working
这是预期的行为。函数 has its own workspace 并且不能直接访问基础工作区中的变量。
您可以尝试使用 evalin
而不是 eval
,并指定 base
工作区:
evalin(ws, expression)
executes expression
, a character vector or
string scalar containing any valid MATLAB® expression using variables
in the workspace ws
. ws
can have a value of 'base'
or 'caller'
to
denote the MATLAB base workspace or the workspace of the caller
function.
但一般来说,有很多理由尽量避免使用 eval
(请参阅 eval
的 MATLAB 帮助),如果您能找到一个获取此数据的不同方式。
我需要访问类型为 Simulink.parameter
:
CAL_vars = dsdd('find','/path/CAL','ObjectKind','Variable','Property',{'name' 'Class' 'value' 'CAL'})
%gets ids of variables in data dictionary
i = 10
for i=1:length(CAL_vars)
var_name = dsdd('GetAttribute',CAL_vars(i),'name');
% gets names of variables in data dict
var_eval = eval(var_name); % this works in standalone script and it does exactly
% what I need, but once i put it in the function I need this for, it returns error
if (length(var_eval.Value) ==1)
if (var_eval.Value == true)
var_eval.Value = 1;
elseif (var_eval.Value == false)
var_eval.Value = 0;
else
end
end
% do something with the Value
if (errorCode ~= 0)
fprintf('\nSomething is wrong at %s\n', var_name)
end
end
出现问题是因为这些结构由 Simulink 制作并给出错误,当我尝试调用 eval(name_of_var): Undefined function 'eval' for input arguments of type 'Simulink.Parameter'.
奇怪的是,它似乎在独立脚本中正常运行,但一旦我将其插入更大的函数,它就会停止工作并开始显示错误
Error using eval
Undefined function or variable 'name_of_var'.
功能在工作区中一目了然。
Curiously, it seems to function properly in a stand-alone script but once I plug it into the larger function, it stops working
这是预期的行为。函数 has its own workspace 并且不能直接访问基础工作区中的变量。
您可以尝试使用 evalin
而不是 eval
,并指定 base
工作区:
evalin(ws, expression)
executesexpression
, a character vector or string scalar containing any valid MATLAB® expression using variables in the workspacews
.ws
can have a value of'base'
or'caller'
to denote the MATLAB base workspace or the workspace of the caller function.
但一般来说,有很多理由尽量避免使用 eval
(请参阅 eval
的 MATLAB 帮助),如果您能找到一个获取此数据的不同方式。