MATLAB: Interp1 and Index exceeds the number of array elements (0)

error messageinterp1

Hello,
Would you please assist me with the following program
for i=1:length(T_HCRM)-2
for j=1:phaz
comX1(i,:)=interp1([1:length(comXS(T_HCRM(i):T_TORM(i)))],comXS(T_HCRM(i):T_TORM(i)),[1:(length(comXS(T_HCRM(i):T_TORM(i)))-1)/(phaz-1):length(comXS(T_HCRM(i):T_TORM(i)))],'cubic');
end
end
I always receive this error message: Index exceeds the number of array elements (0). Error in interp1 (line 153) extrapMask = Xq < X(1) | Xq > X(end);
The mentioned lines are part of a program and has to normalize comXS which is a matrix of one column and 1911 rows to equal sections of 100 frames. Somebody else who is not available provided the program on a database for students but everybody has to change it according to his project.
As I understood from help of MATLAB, the 3 parts of the interp1 function (I mean parts separated by commas) have to have the same lengths. While the first two parts of the interp1 function (parts before the 1st ( length(comXS(T_HCRM(i):T_TORM(i))) ) and the 2nd comma (comXS(T_HCRM(i):T_TORM(i)) )) have equal length AND different part of the 3rd part have equal length, I do not understand why the total length of the 3rd part is 532! I mean length of length (comXS(T_HCRM(i):T_TORM(i))) is 537 and length of comXS(T_HCRM(i):T_TORM(i)) is 537. These parts agait are used in the 3rd part (part located between 2nd comma and 3rd comma). But surprizingly, total length of the 3rd part " length(comXS(T_HCRM(i):T_TORM(i)))-1)/(phaz-1):length(comXS(T_HCRM(i):T_TORM(i))) ".
As you are professionals, maybe the way that I asked you my question is not the best way, but believe me, after spending several hours to find a solution, I am really confused and frustrated with this issue. I hope your knowledge and experience assist me. I am a beginner.
Thank you so much,

Best Answer

Sometimes it is easier to use temporary variables to make things clearer.
for i=1:length(T_HCRM)-2
idx = T_HCRM(i):T_TORM(i);
L = length(idx);
assert(L>0, sprintf('T_TORM(%d) = %f is before T_HCRM(%d) = %f', i, T_TORM(i), i, T_HCRM(i)));
dx = (L-1)/(phaz-1);
for j = 1:phaz
comX1(i,:) = interp1(1:L, comXS(idx), 1:dx:L, 'cubic');
end
end
Now as you look at this, you should ask yourself why you are looping over j but never using j inside the loop.
You should also ask yourself whether it guaranteed that T_HCRM(i):T_TORM(i) will always be the same length -- because if it is not, then some of the rows of comX1 would want to be different lengths than others.
In order to do the assignment of multiple rows, you need to guarantee that each row will be the same length -- and if you can make that guarantee then you should move the calculation of that length outside the loop.
I would suggest that if you are trying to force there to be phaz-1 or phaz entries in length(comXS(T_HCRM(i):T_TORM(i)))-1)/(phaz-1):length(comXS(T_HCRM(i):T_TORM(i))) that you use linspace, such as
L = length(T_HCRM(1):T_TORM(1));
assert(L>0, sprintf('T_TORM(1) = %f is before T_HCRM(1) = %f', T_TORM(1), T_HCRM(1)));
for i=1:length(T_HCRM)-2
idx = T_HCRM(i):T_TORM(i);
comX1(i,:) = interp1(comXS(idx), linspace(1, L, phaz), 'cubic');
end