Filter löschen
Filter löschen

Undefined function or variable 'fileID'.

2 Ansichten (letzte 30 Tage)
Gary
Gary am 12 Nov. 2014
Beantwortet: Image Analyst am 12 Nov. 2014
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)

Antworten (2)

Star Strider
Star Strider am 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)
  2 Kommentare
Gary
Gary am 12 Nov. 2014
Thanks Star Strider,
that solved it. I also had sizeA/B capitalised in reference.
Star Strider
Star Strider am 12 Nov. 2014
My pleasure!

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 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.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by