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

18 views (last 30 days)
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 Comment
Torsten
Torsten on 6 Nov 2022
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 ?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Nov 2022
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.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!