How do I provide input to a Simulink model without placing it in the workspace

前端 未结 4 1557
小鲜肉
小鲜肉 2020-12-29 12:38

I have a Simulink model that is currently being run from a script (i.e. not a function). The script writes variable values to the MATLAB workspace, runs the model simulation

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 13:37

    It's not obvious, but you can input/output data from the sim() command and a calling function's workspace. I've done it before & have an example at work but can't get there until Monday to verify. However, try the solution listed on Mathworks's site:

    Solution:

    When using variable mask parameters in Simulink, the base workspace is the default source workspace of Simulink. However, by using the SIMSET command, this workspace can be changed. SIM is then used with this options structure created by SIMSET. The following is an example on how to do this.

      options = simset('SrcWorkspace','current');
      sim('modelname',[],options)
    

    ...although apparently this got deprecated in R2009b due to incompatibility with the Parallel Computing Toolbox >:( Looks like the correct solution is to explicitly push variables into the simulation's model workspace (different than the base workspace), using assignin().

    http://www.mathworks.com/matlabcentral/newsreader/view_thread/292544

    You have 2 options:

    1. For releases before R2009b, look at the SIMSET documentation. It allows you to set the "SrcWorkspace" property to "current" to use the data from your function.

    http://www.mathworks.com/support/solutions/en/data/1-1BWDA/?solution=1-1BWDA

    1. In newer releases, this option is deprecated because it is not compliant with the Parallel Computing Toolbox and PARFOR. What I recommend is:

    http://www.mathworks.com/support/solutions/en/data/1-ASPEIV/?solution=1-ASPEIV

提交回复
热议问题