fopen gives -1. How to open??
19 次查看(过去 30 天)
显示 更早的评论
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??
0 个评论
采纳的回答
Jan
2012-6-28
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!