在Simulink运行时加载MATLAB功能块的代码
我想在我的MATLAB路径中有一些文件myfunc.m
,并以某种方式将其内容加载到
I would like to have some file, myfunc.m
, in my MATLAB path and somehow load its contents into a MATLAB function block automatically just before the simulation starts. This way, I can use an external editor to write these embedded function, version control them separately as independent files, etc.
有没有办法以编程方式实现这一目标?
Is there a way to achieve this programmatically?
我最初的尝试是尝试使用get_param
之类的东西来访问功能块的内容,但是我似乎无法获得对代码本身的读/写访问权限.
My initial attempt was to try and access the contents of the function block using something like get_param
, but I can't seem to gain read/write access to the code itself.
如果目标MATLAB Function
块尚不存在,则可以按如下所示添加它(请参见
If the target MATLAB Function
block doesn't already exist then you can add it as follows (see this SO post):
load_system('eml_lib');
libname = sprintf('eml_lib/MATLAB Function');
add_block(libname,'myModel/myBlockName');
然后您可以使用Stateflow API修改块的代码:
You can then modify the block's code using the Stateflow API:
sf = sfroot();
block = sf.find('Path','myModel/myBlockName','-isa','Stateflow.EMChart');
block.Script = 'Your code goes here';
另请参见有关MATLAB答案的帖子.