Error in Simulink function block code: Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.
Show older comments
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

1 Comment
Walter Roberson
on 2 Mar 2022
I am concerned about the fact that the for loop overwrites all of y1 each iteration.
Accepted Answer
More Answers (1)
Walter Roberson
on 1 Mar 2022
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
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!