matlab - 如何在不将输入放入工作区的情况下向 Simulink 模型提供输入

标签 matlab simulink

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

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

最佳答案

这并不明显,但您可以从 sim() 命令和调用函数的工作区输入/输出数据。我以前做过,并且在工作中有一个例子,但要到星期一才能到那里进行验证。但是,请尝试列出的解决方案 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)

...虽然显然由于与并行计算工具箱不兼容,这在 R2009b 中被弃用了 >:( 看起来正确的解决方案是使用 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

关于matlab - 如何在不将输入放入工作区的情况下向 Simulink 模型提供输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5279199/

相关文章:

matlab - 如何获得矩阵的对角线并将它们连接起来?

Matlab 命名空间

c++ - 将 Matlab 扰码器函数翻译成 C

matlab - 验证相机校准仍然有效

matlab - 通过减小最大向量的大小,使不等长向量的长度相等

matlab - 在 Simulink 中以编程方式标记信号

matlab - 即使在 Simulink 中使用过去的 SimState 时也可以设置自定义随机数生成器种子

matlab - Simulink:同步和定时

linux - 如何在Windows下使用Matlab嵌入式编码器为LINUX编译?

matlab - 将 .csv 数据导入矩阵