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

조회 수: 29 (최근 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에서 Inputs에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by