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

조회 수: 6 (최근 30일)
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

채택된 답변

TADA
TADA 2018년 11월 8일
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
srinivasan ravi
srinivasan ravi 2018년 11월 11일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Anurag Kammari
Anurag Kammari 2022년 6월 16일
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);

카테고리

Help CenterFile Exchange에서 Scopes and Data Logging에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by