![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1643036/image.png)
Why are initial input values zero for Interpreted MATLAB Fcn block?
11 次查看(过去 30 天)
显示 更早的评论
When using an Interpreted MATLAB Fcn block, I noticed that when I run the simulation, on the first call to the function the inputs are set to zero, regardless of their actual values. For instance the value might be supplied by a constant set equal to 1, but on the first call to Interpreted function block, its value will be zero.
I have attached a simple example to demonstrate the problem. The function, myfun(u), called by the interpreted function block, assigns an output value equal to the input.
function out = myfun(u)
out = u
Note that in the attached example, even though the input to the interpreted function block is assigned a value of 1, by the constant block, when you run the simulation, the first output (printed to the command window) will infact be 0, and after that is correctly computed to be equal to 1.
You can also see this if you put a breakpoint with the debugger inside of myfun. The Simulation stops in the Compilation Phase (probably a clue as to why the inputs aren't assigned yet) and the inputs can be seen to be zero.
If anyone can please explain further what is going on and/or how to avoid this problem (It can cause issues with functions failing due to trying to compute outputs when all inputs are assigned to zero) I would appreciate it. I don't remember having problems with this before. Is this something new, or did I just never notice it?
By the way, I do see that interpreted MATLAB functions will soon be deprecated, but I have some legacy code that still uses them.
0 个评论
采纳的回答
Saurabh
2024-3-15
Answer
HI Jon,
It seems like when the provided Simulink model is executed, the first output you get is always zero, regardless of its actual value.
I attempted to replicate the problem on my end and encountered a comparable circumstance.
Thus, I experimented with modifying a few block parameters of the "Interpreted MATLAB function" block and discovered that, regardless of the actual value, the initial value is always zero when the output dimension is set to -1 (the default value).
This is because the function or expression is executed by Simulink once before the simulation begins when output dimension is set to -1.
I was not receiving zero as my first output when I changed the value from -1 to another value.
I've included a screenshot of the block parameter for the Interpreted MATLAB Function block for your reference:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1643036/image.png)
Note that the dimensions of the output must match the dimensions of the result that the expression or function in the MATLAB function field returns.
I hope this was helpful.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!