How to fix this error while creating a Matlab Array
2 views (last 30 days)
Show older comments
Reference to non-existent field 'path' Error in work2 (line 10) faceData(i).image=imread(faceData(i).path);
2 Comments
Accepted Answer
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
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...
More Answers (0)
See Also
Categories
Find more on File Operations 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!