fopen gives -1. How to open??
조회 수: 13 (최근 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!