Load code for a MATLAB function block at Simulink runtime

你离开我真会死。 提交于 2019-12-01 08:32:13
Florian Brucker

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

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

See also this post on MATLAB Answers.

First, you will need to add the folder containing the m-file to the default path. To do this:

(In the Command window) Go to File -> Set Path -> Add Folder (choose the folder containing the m-file)

Now, you should use the InitFcn callback in the model properties to call your function. To do this, open the model:

(In the Model window) Go to File -> Model Properties -> Callbacks -> InitFcn In the edit box provided for the InitFcn, write the command to call your function i.e. myfunc(); You will have to modify this command as per your function and requirements.

Once done, apply the changes to the Model Properties window and simulate the model.

I am thinking that model callbacks may be a way to do what you want, though I have not used this technique myself.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!