Error while reading files from a directory
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Here is my code:
awdFiles = dir(fullfile(uigetdir,'*.awd'))
for k = 1:length(awdFiles)
waveform = awdFiles(k).name
fid = fopen(waveform)
A(:,k) = textscan(fid, '%s')
fclose(fid);
end
I am recieving this error:
fid =
-1
??? Error using ==> textscan Invalid file identifier. Use fopen to generate a valid file identifier.
Error in ==> readintest at 8 A(:,k) = textscan(fid, '%s')
The thing I do not understand is when say I have 3 files with the names:
DC282 Ch1.awd DC282 Ch1test2.awd DC282 Ch1test3.awd
The code works fine and reads in each file no problem.
If the names are:
DC282 Ch1.awd DC282 Ch1test2.awd DC282 Ch2.awd
The program throws the error out when trying to open the Ch2 file. It seems the names of the files are giving it issues and I am unsure why. I do not see why it has a problem because it pulls the name of the file correctly, but can't open it. Am I missing something simple? Do I need to reinitialize the waveform variable everytime or something?
Thanks
0 Kommentare
Akzeptierte Antwort
Jiro Doke
am 14 Feb. 2011
It's probably the directory. Are the files in a different directory than where you're running from? Store the directory name and use the full path:
dirname = uigetdir;
awdFiles = dir(fullfile(dirname,'*.awd'))
for k = 1:length(awdFiles)
waveform = fullfile(dirname, awdFiles(k).name)
fid = fopen(waveform)
A(:,k) = textscan(fid, '%s')
fclose(fid);
end
2 Kommentare
Jiro Doke
am 14 Feb. 2011
I'm actually not sure why it worked the first time. Unless you happen to have a file with the same name as the first file somewhere on the MATLAB path.
Try typing "which <filename>" where <filename> is the name of the first .awd file.
If you type "awdFiles.name" on the command line, you'll see that it only shows the file names, not the full path. So you always have to reconstruct the full path using the directory name and the file name when you use it.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu File Operations 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!