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

8 投票

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 ExchangeEvent Functions についてさらに検索

製品

リリース

R2010a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by