File name is too long using IMREAD in GUI

7 次查看(过去 30 天)
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.

采纳的回答

Matt Fig
Matt Fig 2011-6-22
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 个)

Walter Roberson
Walter Roberson 2011-6-22
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.

D.
D. 2014-4-14
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.

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by