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

28 次查看(过去 30 天)
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

采纳的回答

Fangjun Jiang
Fangjun Jiang 2022-3-1
编辑:Fangjun Jiang 2022-3-1
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 个评论
Walter Roberson
Walter Roberson 2022-3-2
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.
Fangjun Jiang
Fangjun Jiang 2022-3-2
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];

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-3-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);

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by