Why do I receive the error "Inavlid file identifier" when using fopen?

3 ビュー (過去 30 日間)
Leah
Leah 2012 年 7 月 9 日
コメント済み: Eldhose Poulose 2020 年 5 月 9 日
function spec = abc(filelist);
filelist='filelist23.txt';
fl = importdata(filelist);
nFile = size(fl,1);
%fid = fopen(file);
Nbins = 1024;
spec = zeros(nFile, Nbins);
for iFile = 1:nFile
fid=fopen(fl{iFile})
% Read the 18 header text lines (but not the entire line, and discard)
for i=1:18 hdr=textscan(fid,'%s %*[^\n]',1); end
x=textscan(fid,'%d%d','delimiter',',,');
x = x{:,1};
x = reshape(x,2,Nbins);
spec(iFile,:) = x(2,:);
fclose(fid);
end
end
I get an error at fopen--I get the number -1 which is inccorect and i have no idea how to fix it. The error message is:
??? Error using ==> textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in ==> rdUCS30MCA at 23
for i=1:18 hdr=textscan(fid,'%s %*[^\n]',1);
  2 件のコメント
Jan
Jan 2012 年 7 月 16 日
Not nice, Leah. You found out that you cannot delete a thread after somebody has answered. Then you decided to overwrite the text by nonsense. This is a misuse of the idea of a public forum.
Randy Souza
Randy Souza 2012 年 10 月 18 日
I restored the original question based on Lucas' assertion that this question might help others.

サインインしてコメントする。

回答 (2 件)

Jan
Jan 2012 年 7 月 10 日
Every fopen() should be accompanied by a check of the results:
fid = fopen(fl{iFile});
if fid == -1
error('Author:Function:OpenFile', 'Cannot open file: %s', fl{iFile});
end
File access is prone to problems. A stable program catchs them and the time wasted by debugging can be reduced, if an exhaustive error message is displayed.

Image Analyst
Image Analyst 2012 年 7 月 9 日
My guess would be that perhaps you didn't specify the folder in the list, and the files don't exist on the search path.
  1 件のコメント
Eldhose Poulose
Eldhose Poulose 2020 年 5 月 9 日
addpath("path_where_your_files_are_taken") will solve it

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by