在Simulink运行时加载MATLAB功能块的代码 [英] Load code for a MATLAB function block at Simulink runtime

查看:196
本文介绍了在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答案的帖子.

这篇关于在Simulink运行时加载MATLAB功能块的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆