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

35 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Oct 2021
Edited: MathWorks Support Team on 20 Oct 2021
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".

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Tags

Products


Release

R2010a

Community Treasure Hunt

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

Start Hunting!