Undefined function or variable 'fileID'.
8 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Star Strider
on 12 Nov 2014
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
on 12 Nov 2014
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 Comments
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!