Why does my MATLAB Function block throw "index expression out of bounds" errors?

When using MATLAB Function blocks, if I try to access certain elements of the input parameter, I receive an error message similar to the following:
ERROR: Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.
This is unexpected since I know that the input parameter is a vector, even though the error message states that it is a scalar.

 채택된 답변

MathWorks Support Team
MathWorks Support Team 2026년 5월 4일 0:00
편집: MathWorks Support Team 2026년 5월 4일 6:30
Even though you have designed the model such that the input to the MATLAB Function block is a vector, you will receive this error message if Simulink cannot successfully propagate the signal widths to the MATLAB Function block input (in which case it defaults to a signal width of one). This can sometimes happen for MATLAB Function blocks that are inside a loop, where the output of the MATLAB Function block eventually feeds back to the input of the same MATLAB Function block.
Have a look at the attached example model ("example.mdl") to see a demonstration of this error message. To resolve the error, you need to add more information about the dimension of the signals in your model.
For example, in this case, you can do one of the following (this is not an exhaustive list of solutions):
1. Open the MATLAB Function block editor, and click Edit Data. Select the input variable "u", and set the Size to 3. See "solution1.mdl".
2. Place a Signal Specification block in the feedback path, explicitly telling the MATLAB Function block that this signal has a width of 3. See "solution2.mdl".

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Event Functions에 대해 자세히 알아보기

제품

릴리스

R2010a

태그

Community Treasure Hunt

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

Start Hunting!

Translated by