What is the cause of undefined variable or function error.

Hello and a good day. I just wanna ask a question about 'undefined variable or function' error.
i got an error saying
"Undefined variable or function Target might refer to the function target."
I'm just curious. What is the main cause of this type of error ?
Because, previously my function worked just fine and i haven't change a thing and suddenly this error popped up. Thanks in advance...

Réponses (1)

MATLAB is case sensitive. Target, target, tarGet, TarGet are all different things. If your function is named target, don't call it as Target.
clear all
Tar = 6;
TAR = 7;
tar = 8;
whos

9 commentaires

Thanks Matt, yes i realize that MATLAB is case sensitive. But i dont think i've overlooked that. Because my function worked great before and i haven't changed anything since. too bad my code is to long and has many function calls, quite reluctant to post it here. Any other suggestions ?
which -all Target
See if it is on your path.
Try executing
rehash
which -all Target
returns "C:\Program Files\MATLAB\R2009a\toolbox\eml\eml\@eml\target.m % eml method" in the command window.
'target.m' is not my function tough. the 'Target' that im using is from
fid = fopen('C:\Users\tarmizi\Documents\MATLAB\Feature Extraction MSc\Target_Test.txt','r');
Target = fscanf(fid,'%d');
is this the cause of the confusion ? how do i fix this ? do i have to change the 'Target' to another variable name..
i've changed the 'Target' to 'fileOpen' as below:
fid = fopen('C:\Users\tarmizi\Documents\MATLAB\Feature Extraction MSc\Target_Test.txt','r');
fileOpen = fscanf(fid,'%d');
now MATLAB produces the error:
Undefined function or variable "fileOpen".
the previous problem seems to show that MATLAB was confused between 'Target' and the target.m function. But now MATLAB says that "fileOpen" is undefined.
all the functions are placed in the same folder, so i don't think its a path problem.
Ah, okay then what it is telling you is that at the point where fileOpen is being used (not the point it is being initialized), fileOpen does not exist. We would need to see the code between the two points to explain what is going on.
The following is the code..
function TestNN(test_Feat,netName,net, TEST,featName)
test_Feat = test_Feat;
%=======Test the trained network with Testing features=====
Yt=sim(net,TEST);
%=========================================================
if length(test_Feat.testFeat.TEST)== 416
fid = fopen('C:\Users\tarmizi\Documents\MATLAB\Feature Extraction MSc\Target_Test.txt','r');
fileOpen = fscanf(fid,'%d');
elseif strcmp(test_Feat.Name,'TEST_F8.mat')==1
fid = fopen('C:\Users\tarmizi\Documents\MATLAB\Feature Extraction MSc\PatternF8.txt','r');
fileOpen = fscanf(fid,'%d');
end
disp('SAVE RESULT TO .txt FILE')
Answer = zeros(length(Yt));
L = length(Yt); %number of test pattern
ans = 1;
for m = 1:L
max = 0.0; %0.0
for n = 1:26
if (Yt(n,m)>= max)
max = Yt(n,m);
ans = n;
Answer(m) = ans;
end
end
end
T = Answer;
match = 0;
for i = 1:numel(fileOpen) % change this value if length of Yt changes
if (T(i) == fileOpen(i))
match = match+1;
end
end
RR = (match / L)*100;
fid = fopen('C:\Users\tarmizi\Desktop\tryJa\result.txt','a');
fprintf(fid,'%s:',featName);
fprintf(fid,'{%s}->',netName);
fprintf(fid,'recognition: %.2f \t',RR);
fprintf(fid,'Match: %.2f \t',match);
fprintf(fid,'Total pattern: %.2f \n',L);
%}
disp('END OF EVALUATION, CHECK THE ".txt" FILE GENERATED FOR RESULTS')
fclose('all');
Sorry for the messy code. But that's it. The thing that borders me is that, before this, i had no problem in using it. This is quite odd...
You have if, elseif, but if neither condition is true then fileOpen is left undefined.
yep, let me try to check on that. i'll try some adjustments to that part...
thanks Walter. i've change some of my if else statement. It works !

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by