MATLAB: Error while reading files from a directory

directoryfilereading filesreading from directoryreading multiple files

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

Best Answer

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