Undefined Variable f as i am trying to extract the features

1 view (last 30 days)
clc
clear
close all
format compact
face_folder='pos/';
folder_content=dir([face_folder,'*']);
face=size (folder_content,1);
for k=3:face
k1=k-2
string=[face_folder,folder_content(k,1).name];
image=imread(string);
f(k1,:)=cohog(image);
end
face=face-2;
x=[f(1:face,:);]';
y=[ones(face,1);]';
save testinria x y face
This is my code through which i want to extract the features, but while executing the program it shows error as
Undefined variable f.
I have defined f within the for loop, then why it's showing this error, please help me to rectify the error.
Error in feature1 (line 15)
x=[f(1:face,:)]';

Accepted Answer

Walter Roberson
Walter Roberson on 11 Oct 2012
Because you do not initialize f, you would get that error if dir() returned no more than 2 results.
Note: you should not be assuming that the first two entries should be discarded. dir() does not sort the results, and just returns whatever order is returned by the operating system. In turn, the operating system does not sort the results and returns whatever order is used by the file system. Different file system types on the same machine may return different orders. For example some filesystem types sort in strict ASCII order and so may sort a filename that begins with a space before they sort the '.' directory.
Also, using a variable named "image" will conflict with the image() function.
Adjusted code:
folder_content = dir([face_folder,'*']);
folder_content([folder_content.isdir]) = [];
face = length(folder_content);
f = [];
for k = 1 : face
string = fullfile(face_folder,folder_content(k).name);
thisimage = imread(string);
f(k, :) = cohog(thisimage);
end

More Answers (2)

Image Analyst
Image Analyst on 11 Oct 2012
You need to preallocate f before the loop - at least one row. In your line it doesn't know how many columns there are and it needs to if you're going to assign it like that.
  3 Comments
Image Analyst
Image Analyst on 11 Oct 2012
So, assuming it entered the loop like he says it definitely did, why didn't it create the array f? Instead of saying it was not defined yet, why didn't it just define/create it? I couldn't run the code because I don't have cohog.
My guess would be that it never did enter the loop so by the time it gets to x=[f..... there is no f yet. But he says it was defined in the loop. I wonder if Muzafar knows how to use the debugger.
By the way Muzafar, don't use "image" as the name of your variable - that will blow away the "image()" function that is built in to MATLAB.

Sign in to comment.


Muzafar Pandit
Muzafar Pandit on 12 Oct 2012
Thanks Dear,actually it had a path problem, but you gave me the idea as you pointed "whether it enters the loop or not", and obviously it wasn't entering the loop as face value was 0; thanks guys

Categories

Find more on Graphics Performance 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!