Load code for a MATLAB function block at Simulink runtime

前端 未结 3 1808
名媛妹妹
名媛妹妹 2021-01-14 01:14

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

3条回答
  •  孤街浪徒
    2021-01-14 02:06

    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.

提交回复
热议问题