MATLAB: Variable ‘a’ is undefined on some execution paths.

simulation

when I start simulation, an error message 'Variable 'a_p' is undefined on some execution paths' arise and terminate simulation. I define the variable 'a_p' in that subsystem as following
for j = 1:J
if j==1;
a_p=[3,4,5,9,10];
else
if j==2;
a_p= [1,2,6,7,8,14,15];
else
if j==3;
a_p=[11 12 13 19 20];
else
if j==4;
a_p=[16 17 18 25];
else
if j==5;
a_p=[21 22 23 24];
end
end
end
end
end
end
It would be appreciated if you could help me on simulation running

Best Answer

Both Geoff Hayes and Grieg have made some good points about simplifying the if statements, but it seems no one has addressed the point of the loop itself: if J is five for example this loop will run five times, and calls if a total of fifteen times, and yet the end result is simply that of j=5. This is slow and pointless.
Rather than doing this in unnecessary loops, why not use simpler, neater and faster code? switch would be the obvious choice:
switch J
case 1
a_p = [3,4,5,9,10];
case 2
a_p = [1,2,6,7,8,14,15];
case 3
a_p = [11,12,13,19,20];
case 4
a_p = [16,17,18,25];
case 5
a_p = [21,22,23,24];
otherwise
% error(...) ?
% a_p = [...] ?
end