File name is too long using IMREAD in GUI

3 views (last 30 days)
Hello everyone,
I made a GUI with the help of GUIDE. In one callback, I am reading an image using IMREAD. However, MATLAB throws an error saying that the file name is too long:
??? Error using ==> fopen
Filename is too long.
Error in ==> imread at 366
fid = fopen(filename, 'r');
I know in Windows, MATLAB allows only an extension of 63 characters and I have already considered that. The current path of the script is:
C:\Users\XXXXXXX Demo
and the images' file name are:
image1.bmp
image2.bmp
...
image30.bmp
The code I am using is the following:
function pbuttonStart_Callback(hObject, eventdata, handles)
...
for i = 1 : 20000
if mod(i, 10)==0
x = handles.data(1,:);
if x > 1
x = 1;
elseif x < -1
x = -1;
end
% Define the number of images available for display.
noImages = length(dir('*.bmp'));
m = (1-noImages) / 2; % Define the m.
b = noImages + m; % Define the b.
imageIndex = round((m*x) + b);
% Read the corresponding image.
I = imread(['image' int2str(imageIndex) '.bmp']);
% Display the image on the corresponding axes.
imshow(I, 'Parent', handles.axesSimulation);
% Define current axes and set properties.
axes(handles.axesSimulation);
axes off;
axes image;
drawnow;
disp('Plotted!');
end
pause(0.001);
end
The curious thing is that if I type on console the command, it works perfectly:
I = imread(['render' int2str(imageIndex) '.bmp']);
Beforehand, thanks for your helps!
A.

Accepted Answer

Matt Fig
Matt Fig on 22 Jun 2011
I assume your error is on the line defining I. Put this right before that line and look at what is being passed. You are likely to find what has gone wrong.
['image' int2str(imageIndex) '.bmp']
  2 Comments
Matt Fig
Matt Fig on 22 Jun 2011
Did you discover the cause of the error?

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 22 Jun 2011
There is a possibility that the bit about the file name being too long is not correct. Possibly instead the message should say something like, "too many files open".
I do not know which version(s) it applies to, but there have been reports that imread() [or something in MATLAB triggered by imread()] does not fully return the file open slot to the operating system (e.g., as if it did not fclose() the file), so when many imread() read are done in a loop, you can run out of open files.
  1 Comment
Adriano
Adriano on 22 Jun 2011
Thanks for your answer, will take it into account to.

Sign in to comment.


D.
D. on 14 Apr 2014
I had a similar problem. In my case it was caused by the OS specific behaviour of the "ls" command. Under UNIX it put all files for "imread" in one line of a char-array divided by blank spaces instead of listing them in a 2-dimensional char-array. Unfortunately, on http://www.mathworks.de/de/help/matlab/ref/ls.html I don't see that hint:
1) On UNIX platforms, list is a character row vector of names separated by tab and space characters.
2) On Microsoft Windows platforms, list is an m-by-n character array of names—m is the number of names and n is the number of characters in the longest name. MATLAB pads names shorter than n characters with space characters.
  1 Comment
Walter Roberson
Walter Roberson on 13 Oct 2015
This problem does not exist if you use dir() instead of ls()

Sign in to comment.

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!