Undefined function or variable 'fileID'.
2 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(2 个)
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)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File 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!