how to overcome this error Index exceeds array dimensions. Index value 2 exceeds valid range [1-1] of array x.

1 view (last 30 days)
i am trying to find the skewness value of fft data by setting up the window of width 50 so that it calculates for each window to determine the highest peak(above threshold). I am doing this in Simulink where I directly give input to Matlab function block and getting this error. can you guide me with my code, please.
%main code
function result_k1= fcn(u)
x=[];
x=[x;u];
result_k1=zeros(100,1);
window_width=50;
for j=1:100
window_m1=x((j:window_width+(j-1)));
window_m11=window_m1(:);
result_k1(j)=skewness(window_m11);
end %#codegen end

Accepted Answer

TADA
TADA on 8 Nov 2018
Seems like your input is shorter than your function expects.
You must take the size of u in account when you decide the number of iterations in your loop and the window size, you can have that width 50 as an upper limit, and the index vector j should not exceed the length of u
  1 Comment
srinivasan ravi
srinivasan ravi on 11 Nov 2018
sir,thank you , actually i am trying to implement this function in simulink now i increased the size of number of iteration to 170 it is running but i am getting empty result(NaN). when I load the data into workspace and process that function i am getting the output.
% code
function result_k1= fcn(u)
result_k1=zeros(200,1);
window_width=50;
for j=1:170
window_m1=u((j:window_width+(j-1)));
window_m11=window_m1(:);
result_k1(j)=skewness(window_m11);

Sign in to comment.

More Answers (1)

Anurag Kammari
Anurag Kammari on 16 Jun 2022
function [Sa,Sb,Sc,Tp1] = control(T_ref,sflux_ref,wm,i_meas,Ts, Lm, Lr, Ls, Rs, kr, t_sigma, tr, r_sigma, v, states, p, lambda)
persistent x_opt Fs
if isempty(x_opt),x_opt = 1;end
if isempty(Fs),Fs = 0+0i*1;end
Fs = Fs+Ts*(v(x_opt)-Rs*i_meas);
Fr = Lr/Lm*Fs+i_meas*(Lm-Lr*Ls/Lm);
g = zeros(1,19);
for i = 1:19
v_01 = v(i);
Fsp1 = Fs+Ts*v_01-Rs*Ts*i_meas;
Isp1 = (1+Ts/t_sigma)*i_meas+Ts/(t_sigma+Ts)*...
(1/r_sigma*((kr/tr-kr*1i*wm)*Fr+v_01));
Tp1 = 3/2*p*imag(conj(Fsp1)*Isp1);
g(i)=abs(T_ref-Tp1)+lambda*abs(sflux_ref-abs(Fsp1));
end
[~,x_opt] = min(g);
Sa = states(x_opt,1);
Sb = states(x_opt,2);
Sc = states(x_opt,3);
Caused by: Index exceeds array dimensions. Index value 3 exceeds valid range [1-1] for array 'states'. Error in 'PTCL3/Controller' (line 17) Sa = states(x_opt,1);

Categories

Find more on Simulink Functions 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!