Index into array out of range error in simulink

11 views (last 30 days)
Runtime error: Index into array out of range
Model Name: simm_1
Block Name: simm_1/MATLAB Function
Attempted to access 2 element of data a. The valid index range is 1 to 1
Please note that the simulation will be aborted immediately after you continue from this breakpoint to avoid segmentation violations.
I am a beginner in simulink.Please help me to fix this error . I have attached my simulink model and matlab function
function p1=simm(p)
cp=1;
cp1=5;
a=0.3;
a1=0.4;
b=0.2;
b1=0.4;
for i=1:50
a(i+1)= cp*sin(pi*a1(i))*sin(cp1/a(i));
a1(i+1)= cp*sin(pi*a(i+1))*sin(cp1/a1(i));
end
for i=1:128
a(i+1)= cp*sin(pi*a1(i))*sin(cp1/a(i));
a1(i+1)= cp*sin(pi*a(i+1))*sin(cp1/a1(i));
end
a=a(2:129);
a1=a1(2:129);
for i=1:50
b(i+1)= cp*sin(pi*b1(i))*sin(cp1/b(i));
b1(i+1)= cp*sin(pi*b(i+1))*sin(cp1/b1(i));
end
for i=1:128
b(i+1)= cp*sin(pi*b1(i))*sin(cp1/b(i));
b1(i+1)= cp*sin(pi*b(i+1))*sin(cp1/b1(i));
end
b=b(2:129);
b1=b1(2:129);
N=128;
R=N-1;
for i=1:128
k1=uint8(mod(floor(a*power(10,5)),255)+1);
k2=mod(floor(a1*power(10,5)),R)+1;
p(i,:)=bitxor(p(i,:),k1);
p(i,:)=circshift(p(i,:),[0 k2(i)]);
end
N=128;
R=N-1;
for i=1:128
k1=uint8(mod(floor(a*power(10,5)),255)+1);
k2=mod(floor(a1*power(10,5)),R)+1;
p(:,i)=bitxor(p(:,i),k1');
p(:,i)=circshift(p(:,i),[-k2(i) 0]);
end
N=128;
R=N-1;
for i=1:128
k1=uint8(mod(floor(b*power(10,5)),255)+1);
k2=mod(floor(b1*power(10,5)),R)+1;
p(i,:)=bitxor(p(i,:),k1);
p(i,:)=circshift(p(i,:),[0 k2(i)]);
end
N=128;
R=N-1;
for i=1:128
k1=uint8(mod(floor(b*power(10,5)),255)+1);
k2=mod(floor(b1*power(10,5)),R)+1;
p(:,i)=bitxor(p(:,i),k1');
p(:,i)=circshift(p(:,i),[-k2(i) 0]);
end
p1=p;
end

Answers (1)

Rajanya
Rajanya on 13 May 2025
MATLAB Function Blocks always need to be compatible for code generation but the code provided above has growing arrays which are not supported in the way they are used. It, therefore, fails at this line itself-
a(i+1)= cp*sin(pi*a1(i))*sin(cp1/a(i)); % Growing array 'a'
Currently, Code Generation only supports growing arrays when used with 'end+1' indexing - as mentioned here.
Thanks!

Categories

Find more on General Applications in Help Center and File Exchange

Tags

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!