Undefined function or variable 'fileID'.

조회 수: 8 (최근 30일)
Gary
Gary 2014년 11월 12일
답변: Image Analyst 2014년 11월 12일
I cant understand why this wont work, help please. Both .txt files are in the same folder as the .m file
% Read in time file
A = fscanf(fileID,formatSpec,sizeA)
fileID = fopen('time.txt')
formatSpec = '%f'
SizeA = [1,inf]
% Read in frequency file
B = fscanf(fileID,formatSpec,sizeB)
fileID = fopen('frequency.txt')
formatSpec = '%f'
SizeB = [1,inf]
plot(A,B)

답변 (2개)

Star Strider
Star Strider 2014년 11월 12일
You have to define your statements in the order you use them.
Try this re-ordering and see if it gives you the results you want:
% Read in time file
fileID = fopen('time.txt')
formatSpec = '%f'
SizeA = [1,inf]
A = fscanf(fileID,formatSpec,sizeA)
% Read in frequency file
fileID = fopen('frequency.txt')
formatSpec = '%f'
SizeB = [1,inf]
B = fscanf(fileID,formatSpec,sizeB)
plot(A,B)
  댓글 수: 2
Gary
Gary 2014년 11월 12일
Thanks Star Strider,
that solved it. I also had sizeA/B capitalised in reference.
Star Strider
Star Strider 2014년 11월 12일
My pleasure!

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


Image Analyst
Image Analyst 2014년 11월 12일
It's very dangerous to use the same file ID for two different files if they're both open at the same time, and not to close the flies when you're done. What you should do:
fid1 = fopen(.........
% get stuff out of it.
A = fscanf(fid1, ..........
fclose(fid1);
% Open second file with different file ID
fid2 = fopen(...........
% get stuff out of it.
B = fscanf(fid2,........
fclose(fid2);
You can use the same fid if you close the first file before you open the second file, otherwise use two different file IDs. It's good practice to close the file right after the last call to yank data out of it. And of course you can't even use fid1 before you've called fopen() - that was your original problem.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by