How to fix this error while creating a Matlab Array

2 views (last 30 days)
Reference to non-existent field 'path' Error in work2 (line 10) faceData(i).image=imread(faceData(i).path);
  2 Comments
ISRAA
ISRAA on 9 Oct 2013
Hi Azzi, I am using this code : imageDir=('C:\Users\loona\att_faces\*a.JPG'); faceData=dir(imageDir); fprintf('Reading %d face images from %s...', length(faceData), imageDir); tic for i=1:length(faceData) % fprintf('%d/%d: file=%s\n', i, length(faceData), faceData(i).path); faceData(i).image=imread(faceData(i).path); end fprintf(' ===> %.2f sec\n', toc); fprintf('Saving faceData.mat...\n'); save faceData
So, I am getting this error about .path

Sign in to comment.

Accepted Answer

dpb
dpb on 9 Oct 2013
Edited: dpb on 10 Oct 2013
imageDir=('C:\Users\loona\att_faces\*a.JPG');
faceData=dir(imageDir);
for i=1:length(faceData)
faceData(i).image=imread(faceData(i).path);
end
faceData is a dir structure which has fields
name
date
bytes
isdir
datenum
but no field .path so your attempt to load an image fails because the filename passed in the imread call is null. If as it appears the location isn't on the matlabpath, then you're looking for something like
img=imread(fullfile(fileparts(imageDir),faceDir(i).name));
Don't know why you're trying to add the data to a directory structure...????
  4 Comments
ISRAA
ISRAA on 10 Oct 2013
Thanks dpb, I want to save the data in this way in order to use it later with the next process ,i mean to call the faceData.mat to do other process which is based on strucutre directory. Now, I t shows that the exact number of images (400) in stored in structure array but with this error in saving the data: Reference to non-existent field 'path'.
Error in savedata (line 10) faceData(i).image=imread(faceData(i).path); I have tried many times to change it but no thing is done correctly till now. Please, any help is so appreciated. Thanks.
dpb
dpb on 10 Oct 2013
Edited: dpb on 11 Oct 2013
Reference to non-existent field 'path'.
Error in savedata (line 10) faceData(i).image=imread(faceData(i).path)
Well, that's exactly what I told you first response--there is no path field in a dir structure. I gave you a suitable solution there.
doc dir % for more details
Again, it makes absolutely no sense to try to use the same variable name for the data as you did for the directory structure returned by dir
If you really do need to save these images in an array (and there's still no discernible reason you can't just process them sequentially from anything you've said so far) then make an empty cell array as
imgs=cell(size(faceData)); % create empty cell array of number returned
before the loop and in the loop use
imgs(i)={imread(fullfile(fileparts(imageDir),faceDir(i).name))};
to store each image array in the cell array. Again, I seriously doubt you really, really, really need to do this but if you're adamant...

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!