MATLAB Answers


IMREAD error: Unable to determine the file format

Asked by Michael Martin on 5 Jan 2013

I am having trouble getting a code to finish without error. The error message is with IMREAD.

Here's an overview of my code & problem:

I have 41 images in 9 different folders. My code iterates through each folder, and thus each image. During the iteration IMREAD reads an image, alters it, then IMWRITE writes the altered file in the same directory under a different file name such that the original images and new written images are now in that directory.

The image directories look like this:


In each directory are 41 images. All .tiff files. Each set of images also have the same naming scheme: 0.tiff : 40.tiff

The code successfully iterates through /.../Test2/3

At /.../Test3/1 images 0:9 are ran through the code, then this error message comes up:

Error using imread (line 381)
Unable to determine the file format.
Error in pathwayVersion02 (line 36)


Here is my code:

R = {'3' '3' '3'};
r = length(R);
folderbase = {'Test1','Test2','Test3'};
filepath = '/Users/imgcompute/Desktop/Mock2/';
m = length(folderbase);
B = rdir('/Users/imgcompute/Desktop/Mock2/Test2/Half_Frames/');
lengthB = length(B);
   for j = 1:m
       Half_Frames = 'Half_Frames/';
       filename1 = strcat(filename1, Half_Frames);
       for rr = 1:r
           rr = num2str(rr);
           C = char(strcat(filename1,rr,'/'))
           file_names = rdir(C);
           for s = 1:length(file_names)
               if file_names(s).bytes >=10000;
                  file_count = num2str(file_count);
                  a=['This is pic num',num2str(file_count)];
      % code


Log in to comment.

2 Answers

Answer by Walter Roberson
on 5 Jan 2013


dbstop if error

at the command line, and re-run. When the program stops, determine the file name that is giving the problem. Try to imread() that file at the command line. Copy the filename, and close MATLAB and restart, and then try to imread() that same file. If both times the read fails, you can be sure that it is something in the file rather than a difficulty with running out of some kind of resource because of the loop.

There is a possibility that you might find an unexpected file in that directory that is not an image file.

If it does look like a real image file, does Windows Explorer have problems opening it?

Try using imfinfo() on the file. Try it including 'tiff' as the format, and try it with 'jpg' as well, in case the file is a real image but not a tiff image.


Thanks for the advice! I ran the code, isolated the error and ran IMREAD on the image file I was getting an error on...It turns out that when my code runs through the directory list, I am getting the error because the iteration step is reading the hidden ".DSStore" file that's in the directory. So, the problem was with the images, but rather the file IMREAD was trying to read was hidden.

I thought I accounted for it by my IF statement (.DSStore files had low byte values), but apparently I was wrong. This file, as well as the "." and ".." files that are in every directory are difficult to deal with, but I think I found my solution.

Thank you!


I still don't know why you don't just set up the file pattern so that you get only files with the proper extension like I suggested.

Log in to comment.

Answer by Image Analyst
on 5 Jan 2013

Replace this line:

file_names = rdir(C);

with this:

file_names = dir('*.tif');

and try again. And then this line - not sure what problems that might cause with the null prepended - maybe none, but try this more robust way.

folder = filename1;
fullFileName = fullfile(folder, file_names(s).name);
oldimage=imread(fullFileName );

Also, you might want to look at genpath() to generate all subfolders automatically.

  1 Comment

['',string] is the same as string, provided that string is char data type -- even if string is a strangely-sized empty string such as ones(1,1,0)

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!