Undefined function or variable 'fileID'.

1 次查看(过去 30 天)
Gary
Gary 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.

请先登录,再进行评论。


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 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by