如何保存在 MATLAB simulink 操作过程中生成的值
How to save a value generated in middle of a MATLAB simulink operation
我成功地建立了 MATLAB Simulink 模型 运行。在这个模型中,一个函数运行代码来完成操作。函数代码在运行过程中产生一个值。我想保存这个值并在后续操作中使用。
在上面的屏幕截图中,Icur_in 和 Icur_ou 是函数的输入和输出。实际上两者都指的是相同的值。我正在使用记忆功能来保存下一次操作的值。到目前为止还好。但价值不断变化。
我现在的密码是:
Function [Icur_ou] = fun(Icur_in)
Icur_ou = Icur_in;
if somecondition
Icur_ou = I_s;
end
end
我不是 100% 确定你的问题是什么,因为你说:"Up to this point is fine." 而且你的模型对我来说看起来不错,但我发现 this link, which seems to be about the same problem. Besides the suggested solutions, I also liked the first two comments about persistent 变量,我认为这也可能对您有所帮助,具体取决于模型的采样时间(请参阅 link 中的评论)。
在这种情况下,您不需要内存块,而是必须将 Icur_
变量设为 persistent
,以便它在函数调用之间保留在内存中。类似这样:
Function [Icur_ou] = fun(Icur_in)
persistent Icur_ou = Icur_in;
if somecondition
Icur_ou = I_s;
end
end
我成功地建立了 MATLAB Simulink 模型 运行。在这个模型中,一个函数运行代码来完成操作。函数代码在运行过程中产生一个值。我想保存这个值并在后续操作中使用。
在上面的屏幕截图中,Icur_in 和 Icur_ou 是函数的输入和输出。实际上两者都指的是相同的值。我正在使用记忆功能来保存下一次操作的值。到目前为止还好。但价值不断变化。
我现在的密码是:
Function [Icur_ou] = fun(Icur_in)
Icur_ou = Icur_in;
if somecondition
Icur_ou = I_s;
end
end
我不是 100% 确定你的问题是什么,因为你说:"Up to this point is fine." 而且你的模型对我来说看起来不错,但我发现 this link, which seems to be about the same problem. Besides the suggested solutions, I also liked the first two comments about persistent 变量,我认为这也可能对您有所帮助,具体取决于模型的采样时间(请参阅 link 中的评论)。
在这种情况下,您不需要内存块,而是必须将 Icur_
变量设为 persistent
,以便它在函数调用之间保留在内存中。类似这样:
Function [Icur_ou] = fun(Icur_in)
persistent Icur_ou = Icur_in;
if somecondition
Icur_ou = I_s;
end
end