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

if true
% code
end
[head,nw,wv,spec]=ReadOOIfile('/Users/elkel/Active','D02e1.txt')
% [head,nw,wv,lt]=ReadOOIfile(dirn,filename);
% read, smooth, cut to 300-700nm every 2nm, for OOI USB2000+ output spectra
% DATA: dirn : scan file directory, such as 'C:\Scans\'
% fname: scan file name
% OUTPUT:
% head: file name, 'date & time', integ. time, n averaged, boxcar
% nw: points in scan wv = wavelengths spec = spectrum output
wv=(300:1:700)'; n=length(wv);
fnm=[D02e1.txt];
fid=fopen(fnm);
rd=textscan(fid,'%s',17,'whitespace','\n'); heads=rd{1};
% heads contains first 17 rows of file--OOI scan parameters
% extract=date, integration time, averaged, boxcar
rd=textscan(fid,'%f %f');
fclose(fid);
Hi, I'm trying to figure out why I get the above error code when using textscan. I think it's something to do with my specification of the filename (fnm) but I can't seem to get it to work - how should the filename be specified? Directories all seem fine so any help much appreciated.

답변 (1개)

Mischa Kim
Mischa Kim 2014년 2월 17일
편집: Mischa Kim 2014년 2월 17일
Laura, use
fnm = 'D02e1.txt';
fid = fopen(fnm,'r');
The filename needs to be a string of characters.

댓글 수: 5

Thanks for that, I'm afraid the error message is still coming up. My current folder and userpath are the same, is there anything else I might be missing?
Could you attach the entire code and .txt file? See paper clip logo.
Another thing you can try:
fnm = 'D02e1.txt';
fid = fopen(fnm,'r');
if fid == -1
error('Author:Function:OpenFile', 'Cannot open file: %s', fnm);
end
which will result in an error msg if the file cannot be opened as expected.
Data file attached. Many thanks for your help.
There are a couple of things:
  • Change the function definition to [head,nw,wv,spec]=ReadOOIfile() and remove the semi-colon. You could read the file name as a parameter, but then you would use [head,nw,wv,spec]=ReadOOIfile(fnm), and without hardcoding it inside the function.
  • Replace fname by fnm in head=[fnm ' ''' datestr ''' ' IT ' ' AV ' ' BX]
  • Comment out the two clear commands. In one instance you are clearing a variable which you later try to access.
Once you get all this changed it'll work.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

질문:

2014년 2월 17일

편집:

2014년 2월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by