Clear Filters
Clear Filters

fopen gives -1. How to open??

29 views (last 30 days)
Brenden
Brenden on 28 Jun 2012
My current work involves image processing. Just recently I have been looking at archived files from the early 2000's. The problem is that these images are stored as *.FILT, *.filt or *.im.... To my knowledge these filetypes where created by a past colleague inorder to save the image data with a text header (information about the image). My problem now is that these images cannot be opened as the software is far out of date.
I have written a small code to convert these files into *.bmp as follows:
clear all
close all
% Prompt for image file
[image_file image_file_path image_file_filterindex] = uigetfile({'*.FILT;*.filt;*.im;*'}, 'Select image for conversion to *.bmp','MultiSelect','On');
if iscell(image_file) ==1
for n=1:length(image_file)
[pathstr, name, ext] = fileparts(image_file{n});
file_id = fopen(image_file{n})
if file_id ~= -1
header = fread(file_id,8192,'char');
image = fread(file_id,[1024,1024],'uint16',0,'b');
% image = fread(file_id,[1024,1024],'int16',0,'n');
image = image./max(image(:));
fclose('all');
imwrite(image, [image_file_path name '_2_uint16_b' '.bmp'], 'bmp');
end
end
elseif iscell(image_file) ==0
[pathstr, name, ext] = fileparts(image_file);
file_id = fopen(image_file)
if file_id ~= -1
header = fread(file_id,8192,'char');
image = fread(file_id,[1024,1024],'uint16',0,'b');
% image = fread(file_id,[1024,1024],'int16',0,'n');
image = image./max(image(:));
fclose('all');
imwrite(image, [image_file_path name '_2_uint16_b' '.bmp'],'bmp');
end
end
My problem is that fopen gives -1 most of the time and so I cannot even open the file. However I have just recently found by running the program a few times on the same file that sometimes fopen gives -1 and sometimes it gives a valid id and the conversion goes through... Is there something wrong with fopen?? Any ideas??

Accepted Answer

Jan
Jan on 28 Jun 2012
fopen replies -1 when it does not find the file. You can test this:
file_id = fopen(image_file{n});
if file_id == -1
if exist(image_file{n}, 'file')
error('Cannot open existing file: %s', image_file{n});
else
error('Missing file: %s', image_file{n});
end
end
Unfortunately this does not give the correct warning, the you try to open a folder, because exist(S, 'file') checks for folders also. Anyhow, it should help to find the problem.
  4 Comments
Brenden
Brenden on 28 Jun 2012
Thank you Jan. You where right it was not reading the file path. I changed the code as follows:
file_id = fopen([image_file_path '/' image_file{n}]);
Brenden
Brenden on 28 Jun 2012
I have one more question. In the above code I read the data with
image = fread(file_id,[1024,1024],'uint16',0,'b');
where i specify the size of the image as [1024,1024]. Is there anyway to have matlab calculate what size the image is from the file?

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!