My program works on MATLAB, but doesn't work on simulink. The error message says that Ph1 is empty. But it shouldn't be and is not empty on MATLAB. Can someone help me ?
Show older comments
function [M1, M2] = test_sortie(x, y, T, A)
tabtheta = zeros(1,length(T));
for i = 1:length(T) %on crée le tableau avec tous les angles correspondants aux séparations entre les bandes
tabtheta(i) = (pi/2 - atan2(y, T(i,1)-x))*180/pi;
end
Ph1 = [];
Ph2 = [];
[~, Ph2] = find(tabtheta>-3.04 & tabtheta<6.84); %On trouve tous les angles que les capteurs voient
[~, Ph1] = find(tabtheta>-6.84 & tabtheta<3.04);
Ph1calibre = zeros(1,length(Ph1)+2);
Ph2calibre = zeros(1,length(Ph2)+2);
for i = 2:(length(Ph1)+1)
Ph1calibre(i) = round(((tabtheta(Ph1(i-1))+3.8/2)/0.005) + 1975/2 + 1); %On calibre les bons angles
end
for i = 2:(length(Ph2)+1)
Ph2calibre(i) = round(((tabtheta(Ph2(i-1))-3.8/2)/0.005) + 1975/2 + 1); %On calibre les bons angles
end
Ph1calibre(1) = 1;
Ph1calibre(end) = 1976;
Ph2calibre(1) = 1;
Ph2calibre(end) = 1976;
Ph1 = [Ph1, Ph1(end)+1];
Ph2 = [Ph2, Ph2(end)+1];
M1 = 0;
M2 = 0;
for i = 1:length(Ph1)
M1 = M1 + T(Ph1(i),2)*A(Ph1calibre(i+1), Ph1calibre(i)); %calcul des moyennes
end
for i = 1:length(Ph2)
M2 = M2 + T(Ph2(i),2)*A(Ph2calibre(i+1), Ph2calibre(i));
end
end
This is my code. A is a 1976x1976 matrix, and T is a 78x2 matrix.
This program should return two averages, M1 and M2.
This is not my first program, I tried to modify it by iniatializing every variables and by using easier commands but I didn't success. Does someone have an idea of what's my problem ?
Here is the simulink code and the error message :

An error occurred while running the simulation and the simulation was terminated
Caused by:
- Index exceeds matrix dimensions. The array Ph1 is empty and therefore has no valid indices. Error in 'simulinktest/MATLAB Function' (line 33) Ph1 = [Ph1, Ph1(end)+1]; Error in 'simulinktest/MATLAB Function' (line 33)
Accepted Answer
More Answers (0)
Categories
Find more on Simulink 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!