Error using fwrite Invalid file identifier. Use fopen to generate a valid file identifier.

Hello,
This matlab code reads a .raw file with a sequence of images and does small crops of the original one. After cropping, it saves them also in different .raw files with the name indicated.
I've checked the other sections of the program and they work perfectly; I get the correct small crops, but it fails to save them as .raw files.
for k=1:Ny
for i=1:Nx
filename = ['COMPT-EP2-Z40-U0-125x125x261-29298-', num2str(i), num2str(k),'.raw'];
fid=fopen(filename,'w+');
for j=1: length(CROP{1,1}(1,1,:))
fwrite(fid,CROP{k,i}(:,:,j));
end
fclose(fid);
end
end
And I get the following error:
Error using fwrite Invalid file identifier. Use fopen to generate a valid file identifier.
The value I get for fid after running the code is -1. I'm pretty sure the code worked three weeks ago.
What could be the problem??
Thank you very much.

 Accepted Answer

According to the fopen documentation this error occurs "If fopen cannot open the file, then fileID is -1."
Usually this happens because the path has not been specified, or the filename is not correct. On Windows it might be related to permissions for a particular folder or file. Symbolic links may also cause problems.
It could be that the folder does not exist although you try to create a file there. Note that fopen will not create a folder, you have to arrange that yourself.
A common mistake that beginners make is to forget to specify the path to a file, as they mistakenly believe that MATLAB will see every file in the universe and be able to open them, even without specifying any path.
There may be some illegal characters in the string, this can also cause problems.
You can use exist and dir to check the folder contents and permissions.

3 Comments

Thank you very much for the info.
I believe that I am at the correct folder. The first lines of the program are the following:
cd('folder where the file is')
filename1='name of the file';
xsize=1249; % en pixel
ysize=1150; % en pixel
zsize=261; % en pixel
volume=[];
volume=volread(filename1,format,skip,xsize,ysize,zsize,byteorder);
The function "volread" is in charge of reading file approprietaly. Therefore, if the folder was incorrect or the file inexistant, the code wouldn't even run the crops (which it does).
I am also working at linux, maybe I should have commented this before.
Don't use cd for accessing files, instead you should use an absolute or relative path (this is much faster, and is more robust when an error occurs or during debugging).
Try something like this:
strPath = 'path of file';
strName = 'name of file';
%
xsize=1249; % en pixel
ysize=1150; % en pixel
zsize=261; % en pixel
%
volume = volread(strPath, strName, format, skip, xsize, ysize, zsize, byteorder);
and inside your function create the filepath using fullfile:
strFull = fullfile(strPath,strName);
fopen(strFull) % or whatever you use to read the file.
Do the same for the file-write operation, something like this:
newName = sprintf('COMPT-EP2-Z40-U0-125x125x261-29298-%d%d.raw', i, k);
newFull = fullfile(strPath,newName);
fid = fopen(newFull,'w+')
Note that using explicit path and filenames makes it easy to check the existence of the directory and files (see exist and dir).
I suspect that your problem is related to the cd command, and passing a more robust filepath may help you to identify and resolve the problem.
You were really really helpful. Thank you very much!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!