Why are initial input values zero for Interpreted MATLAB Fcn block?

조회 수: 22 (최근 30일)
Jon
Jon 2024년 2월 21일
댓글: Jon 2024년 3월 28일
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.

채택된 답변

Saurabh
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:
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.
  댓글 수: 1
Jon
Jon 2024년 3월 28일
Sorry, I hadn't check back earlier to see your very helpful answer to my question.
I can also confirm that letting the Interpreted MATLAB Function block inherit the dimensions from the output of the function (-1 for Output dimensions), results in using an initial value of zero for the functions inputs. As you say, specifiying the input dimension, to match the actual output dimension, prevents this from occuring.
I see now that in the MATLAB documentation for the Interpreted MATLAB function that there is some further, indication (although not so obvious without hindsight) of potential problems with letting the block inherit dimensions:
"Specify -1 to inherit the dimensions from the output of the specified function or expression. To determine the output dimensions, Simulink runs the function or expression once before simulation starts."
So I guess it is this initial evaluation where it is trying to determine the dimensions, which causes the problem.
Thanks so much for providing your answer to this.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by