Error in Simulink function block code: Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.

I have 25 matrices described by the global variable A1. I just want to test a simple code using Simulink function block but its showing the error:
Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.
More information
Function 'MATLAB Function' (#33.91.92), line 8, column 8:
"2"
function ytest = fcn3(xtest)
global A1
A1 =[0 1.0000;
-1.7677 -2.8133];
A1(:,:,2) =[0 1.0000;
-2.3826 -3.0337];
A1(:,:,3) =[0 1.0000;
-4.4441 -2.8292];
A1(:,:,4) =[0 1.0000;
-6.2452 0];
A1(:,:,5) =[0 1.0000;
-4.4441 2.8292];
A1(:,:,6) =[0 1.0000;
-2.3826 3.0337];
A1(:,:,7)=[0 1.0000;
-1.7677 2.8133];
A1(:,:,8) =[ 0 1.0000;
-5.3838 -1.1431];
A1(:,:,9) =[0 1.0000;
-5.5952 -1.3182];
A1(:,:,10) =[0 1.0000;
-6.5828 -1.6022];
A1(:,:,11) =[ 0 1.0000;
-8.1128 0];
A1(:,:,12) =[0 1.0000;
-6.5828 1.6022];
A1(:,:,13) =[0 1.0000;
-5.5952 1.3182];
A1(:,:,14) =[0 1.0000;
-5.3838 1.1431];
A1(:,:,15) =[0 1.0000;
-8.5324 -0.1750];
A1(:,:,16) =[ 0 1.0000;
-8.5517 -0.2137];
A1(:,:,17) =[ 0 1.0000;
-8.6834 -0.3584];
A1(:,:,18) =[ 0 1.0000;
-9.3679 0];
A1(:,:,19) =[0 1.0000;
-8.6834 0.3584];
A1(:,:,20) =[0 1.0000;
-8.5517 0.2137];
A1(:,:,21) =[0 1.0000;
-8.5324 0.1750];
A1(:,:,22) =[0 1.0000;
-9.8100 0];
A1(:,:,23) =[0 1.0000;
-9.8100 0];
A1(:,:,24) =[0 1.0000;
-9.8100 0];
A1(:,:,25) =[ 0 1.0000;-9.8100 0];
for i=1:25
y=A1(:,:,i)*xtest;
y1=sum(y)
end
ytest=y1

 Accepted Answer

You probably didn't define A1 in the Model Explorer or the Data Editor for the MATLAB function. So it took the first assignment of A1 as its size definition. Thus, A1(:,:,2) is in violation as the third dimension is only 1.
Making the following change should resolve the problem.
A1=zeros(2,2,25);
A1(:,:,1) =[0 1.0000;
-1.7677 -2.8133];

7 Comments

Thank you so much for the sugestion. I added it but still its showin error:
Simulink does not have enough information to determine output sizes for
this block. If you think the errors below are inaccurate, try specifying
types for the block inputs and/or sizes for the block outputs.
Component: MATLAB Function | Category: Coder error
Size mismatch (size [2 x 2] ~= size [2 x 2 x 25]).
The size to the left is the size of the left-hand side of the assignment.
Function 'MATLAB Function' (#23.39.41), line 3, column 1:
"A1"
Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.
More information
Function 'MATLAB Function' (#23.103.104), line 7, column 8:
"2"
Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
I have defined A1 inside Data Store memory in the ports and data manaer and also in the model explorer
You could run your code without Simulink and separately in MATLAB, debug it line by line and you will see the cause of the problem.
function ytest = fcn3(xtest)
global A1
A1=zeros(2,2,25);
A1 =[0 1.0000;-1.7677 -2.8133];
A1(:,:,2) =[0 1.0000;-2.3826 -3.0337];
A1(:,:,3) =[0 1.0000;-4.4441 -2.8292];
A1(:,:,4) =[0 1.0000;-6.2452 0];
A1(:,:,5) =[0 1.0000;-4.4441 2.8292];
A1(:,:,6) =[0 1.0000; -2.3826 3.0337];
A1(:,:,7)=[0 1.0000;-1.7677 2.8133];
A1(:,:,8) =[ 0 1.0000;-5.3838 -1.1431];
A1(:,:,9) =[0 1.0000;-5.5952 -1.3182];
A1(:,:,10) =[0 1.0000;-6.5828 -1.6022];
A1(:,:,11) =[ 0 1.0000;-8.1128 0];
A1(:,:,12) =[0 1.0000;-6.5828 1.6022];
A1(:,:,13) =[0 1.0000;-5.5952 1.3182];
A1(:,:,14) =[0 1.0000; -5.3838 1.1431];
A1(:,:,15) =[0 1.0000;-8.5324 -0.1750];
A1(:,:,16) =[ 0 1.0000;-8.5517 -0.2137];
A1(:,:,17) =[ 0 1.0000;-8.6834 -0.3584];
A1(:,:,18) =[ 0 1.0000;-9.3679 0];
A1(:,:,19) =[0 1.0000;-8.6834 0.3584];
A1(:,:,20) =[0 1.0000;-8.5517 0.2137];
A1(:,:,21) =[0 1.0000;-8.5324 0.1750];
A1(:,:,22) =[0 1.0000;-9.8100 0];
A1(:,:,23) =[0 1.0000;-9.8100 0];
A1(:,:,24) =[0 1.0000;-9.8100 0];
A1(:,:,25) =[ 0 1.0000;-9.8100 0];
for i=1:25
y=A1(:,:,1)*xtest;
y1=sum(y)
end
ytest=y1
Size mismatch (size [2 x 2] ~= size [2 x 2 x 25]).
If your global A1 is currently 2x2 then Simulink cannot overwrite that. The global would need to be preconfigured to the right size or else the global must not exist yet.
A1 =[0 1.0000;-1.7677 -2.8133];
This is problematic. Should be below. It is in my answer.
A1(:,:,1) =[0 1.0000;-1.7677 -2.8133];

Sign in to comment.

More Answers (1)

A1 =[0 1.0000;
-1.7677 -2.8133];
A1(:,:,2) =[0 1.0000;
-2.3826 -3.0337];
In MATLAB Function Blocks, you cannot expand the size of a variable after the first assignment -- not unless you have used coder.varsize .
If you move the assignment
A1(:,:,25) =[ 0 1.0000;-9.8100 0];
to be first, then that will allocate the full size in the first statement.
But better would probably be to insert
A1 = zeros(2,2,25);

Categories

Products

Release

R2015b

Community Treasure Hunt

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

Start Hunting!