How to fix this error while creating a Matlab Array

Reference to non-existent field 'path' Error in work2 (line 10) faceData(i).image=imread(faceData(i).path);

댓글 수: 2

What faceData(i).path represent?
ISRAA
ISRAA 2013년 10월 9일
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

댓글을 달려면 로그인하십시오.

 채택된 답변

dpb
dpb 2013년 10월 9일
편집: dpb 2013년 10월 10일
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

ISRAA
ISRAA 2013년 10월 10일
Hello dpb, Thanks for your answer. But, please I am trying to just build array for 100 images which located in the folder att_faces in the directory : 'C:\Users\loona\att_faces\*a.JPG' so, how to fix it since I tried to change the code but still hard to fix it. If any help from your side will be appreciated.
dpb
dpb 2013년 10월 10일
편집: dpb 2013년 10월 10일
A jpg image will be a 2 (more likely 3) D array already -- what's the purpose or end result intended?
What prevents you from doing whatever it is that's wanted with each in turn?
ADDENDUM:
If there is some reason to try to hold a number of different images in memory at a time (altho you may well run into memory issues, particularly if you're still on a 32-bit OS), the simplest way bookkeeping-wise would be to use a cell array to contain the image arrays. Then each cell 1:N would contain the image; remember then to use it you have to use the curly braces ( "{}" ) to dereference the cell and return the contents thereof or use cellfun() to operate on each cell.
ISRAA
ISRAA 2013년 10월 10일
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 2013년 10월 10일
편집: dpb 2013년 10월 11일
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...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

2013년 10월 9일

편집:

dpb
2013년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by