如何在不将Simulink模型放入工作空间的情况下为Simulink模型提供输入(How do I provide input to a Simulink model without placing

编程入门 行业动态 更新时间:2024-10-24 06:33:58
如何在不将Simulink模型放入工作空间的情况下为Simulink模型提供输入(How do I provide input to a Simulink model without placing it in the workspace)

我有一个Simulink模型,目前正在运行一个脚本(即不是一个函数)。 该脚本将变量值写入MATLAB工作区,运行模型仿真(使用这些值),然后该模型将附加值写入工作区。 如果我尝试将脚本转换为函数( function [output] = runSim()放在文件的顶部),那么Simulink会抱怨它不知道变量,可能是因为它们不在MATLAB中工作区,而是他们在功能范围内。

有没有一种优雅的方式来提供一个Simulink模型的输入,并从Simulink模型中取出输出,而不是将它们粘贴到工作空间中?

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.

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网站上列出的解决方案:

解:

在Simulink中使用可变掩模参数时,基础工作空间是Simulink的默认源工作空间。 但是,通过使用SIMSET命令,可以更改此工作空间。 SIM然后与由SIMSET创建的这个选项结构一起使用。 以下是如何执行此操作的示例。

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

......尽管显然这在R2009b中被弃用,因为与Parallel Computing Toolbox不兼容> :(看起来正确的解决方案是使用assignin()将变量显式推入到模拟的模型工作区(不同于基本工作区assignin() 。

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

你有2个选择:

对于R2009b之前的版本,请查看SIMSET文档。 它允许您将“SrcWorkspace”属性设置为“当前”以使用您的函数中的数据。

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

在较新的版本中,此选项已被弃用,因为它不符合并行计算工具箱和PARFOR。 我推荐的是:

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

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:

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

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

更多推荐

本文发布于:2023-08-01 02:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   不将   放入   情况下   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!