Why am I getting unrecognized function or variable 'length' ??

조회 수: 6 (최근 30일)
Hi!! I am building a code to create a convolutional neural network but for some reason when I call the function used to train the CNN it always raises an error "Unrecognized function or variable 'length'" at the same point.
I don't understand why this happens since "length" in an intrinsic matlab function and its working well on other functions/scripts...
[Ptrain,Ttrain,Trg_train,Ptest,~,Trg_test]=building_trainset_testset(features,new_Trg,T,ind_seizureBeginning); rng(0);
[Ptrain,Trg_train,~]=class_balancing(Trg_train,Ttrain,Ptrain); Ptrain=mat2gray(Ptrain);
%Building 4D matrix (29 (instants)x29 (features) x1 (because images are grey) x numberofImages)
newPtrain=[]; newTrg_train=[]; window_width=29; count=0;
ind_inter=find(Trg_train==1);
ind_preictal=find(Trg_train==2);
ind_ictal=find(Trg_train==3);
ind_posictal= find(Trg_train==4);
% THIS IS WHERE THE ERROR RAISES
for i=1:length(ind_inter)
if i<length(ind_inter)-28
count=count+1;
newPtrain(:,:,1,count)=Ptrain(:,ind_inter(i:i+28));
newTrg_train=[newTrg_train; 1];
end
end
% REST OF THE CODE
newTrg_train=categorical(int64(newTrg_train));
  댓글 수: 1
Torsten
Torsten 2022년 11월 6일
Maybe somewhere in your code you used the name "length" for a variable ?
Try
clear length
at the beginning of the function. Does the error message pertain ?

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 11월 7일
At some point in the same function, you assigned a value to a variable named length . Once you do that, then even if you clear it again, you can no longer use length as a function within the same function.
More exactly:
If you have a variable and call a function with the same name inside a function, then the first version that MATLAB encounters during executing the function "locks in" MATLAB's interpretation of what the name is to be. If you had called the function length() before assigning to a variable named length then when you tried to use length as a variable MATLAB would have complained that it could not find the variable. If you had assigned to a variable named length before you called length as a function, then even if you had cleared the variable, then inside the function you would no longer be able to call the function length. You cannot use the same name as both a variable and a function within the same function.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by