如何在不将Simulink模型输入工作空间的情况下将其提供给Simulink模型 [英] How do I provide input to a Simulink model without placing it in the workspace

查看:405
本文介绍了如何在不将Simulink模型输入工作空间的情况下将其提供给Simulink模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Simulink模型,该模型当前正在通过脚本(即不是函数)运行.该脚本将变量值写入MATLAB工作空间,运行模型仿真(使用这些值),然后模型将其他值写入工作空间.如果我尝试将脚本转换为函数(即通过在文件顶部放置function [output] = runSim()),则Simulink会抱怨它不了解变量,大概是因为它们不在MATLAB工作空间中,而是它们在功能范围内.

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 (which uses these values), and then the model writes additional values to the workspace. If I try to convert the script into a function (i.e. by placing function [output] = runSim() at the top of the file) then Simulink complains that it doesn't know about the variables, presumably because they are not in the MATLAB workspace, but rather they are in the function scope.

除了将它们粘贴到工作区中之外,是否有一种优雅的方法为Simulink模型提供输入并从Simulink模型获取输出?

Is there an elegant way to provide a Simulink model with inputs and take outputs from a Simulink model other than sticking them into the workspace?

推荐答案

这不是很明显,但是您可以从sim()命令和调用函数的工作区输入/输出数据.在&之前,我已经完成了.有一个工作示例,但要等到星期一才能验证.但是,请尝试在Mathworks网站上列出的解决方案:

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:

在 Simulink,基本工作区是 Simulink的默认源工作区. 但是,通过使用SIMSET命令, 此工作空间可以更改. SIM卡是 然后与此选项结构一起使用 由SIMSET创建.以下是 有关如何执行此操作的示例.

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)


...虽然显然是由于与并行计算工具箱不兼容而在R2009b中弃用了此方法> :(看起来正确的解决方案是使用 assignin() .


...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

您有2个选择:

You have 2 options:

  1. 对于R2009b之前的版本,请查看SIMSET文档.它允许 您设置"SrcWorkspace"属性 转换为当前"以使用您的数据 功能.
  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. 在较新的版本中,不赞成使用此选项,因为它不兼容 使用并行计算工具箱 和PARFOR.我推荐的是:
  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

这篇关于如何在不将Simulink模型输入工作空间的情况下将其提供给Simulink模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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