0

I'm writing a script that needs to loop through all the .xls files in a folder and pull data from specific cells. I think xlsread is capable of this, but I keep getting an error that reads:

Matlab:xlsread:filenotfound

Here is the basic structure of the code:

files = dir('C:\folder\folder\folder\*.xls');
channelinfo = 'C:\differentfolder\chanloc.type';
cnt = '.cnt';
condition = 'varriable';


for i = 1:length(files(:,1))
    try

        CNTCrash1WindowStart = xlsread(files(i).name, 'Summary', 'C8');
        CNTCrashWindowEnd = xlsread(files(i).name, 'Summary', 'D8');
        subjectNum = xlsread(files(i).name, 'User Info', 'A2');

        eeglab;
        %eeglab things happen here

    catch ME
        disp(ME)
    end
end            
S. Reaves
  • 35
  • 7
  • 4
    Did you check the existence and content of the `dir` call? I.e. does it find files, and if so, are the file names contained within `files.name`? Could be you're not going to the full path in the `xlsread`, try `tmpname = ['C:\folder\folder\folder\' files(i).name]` and call `xlsread` with that – Adriaan Dec 14 '15 at 14:42
  • 1
    i like to add a point to @Adriaan suggestion.. Check whether your file extension is '.xls' or '.xlsx'? – Mahesh Kumar Kodanda Dec 14 '15 at 15:32
  • They are indeed '.xls' extensions; I was hopeful the solution was that simple. I now suspect that the problem is xlsread is calling in the entire filepath instead of the file name, which I don't think it can handle. Could that be it? How could I remove the extra information so xlsread was only grabbing the file name? – S. Reaves Dec 14 '15 at 15:40
  • 1
    Don't you mean `i = 1:length(files(:,1))`, assuming that you want to got through all of the files? Have you done any debugging? Remove the semicolon on your first line and print out `files(i).name` (this will only be the file name and doesn't include the full path, which you'll need unless the directory in question is your present working directory) inside your loop. – horchler Dec 14 '15 at 16:47
  • Yes, Horchler, I did mean to loop through more than one file. Thanks for spotting thing. Also, @Adriaan, your solution worked well! While xlread is a useful tool, it is pretty picky about accepting just file names with no path attached. Splendid. – S. Reaves Dec 15 '15 at 14:04
  • @S.Reaves good to know it worked. I'll type it up as answer in that case. – Adriaan Dec 15 '15 at 14:12

1 Answers1

2

The problem here is that your files are not located in your pwd (Present Working Directory). The call to dir gives you file names, but not including the full path name. Hence xlsread is looking for your .xls files in a location where they are not. Fix this by setting a temporary name where you include the full path and feed that to xlsread:

folder = 'C:\folder\folder\folder\'; %'//
files = dir([folder, '*.xls']);
channelinfo = 'C:\differentfolder\chanloc.type';
cnt = '.cnt';
condition = 'variable';

for ii = 1:length(files(:,1))
    try
        tempname = fullfile(folder, files(ii).name); %// Thanks to excaza
        CNTCrash1WindowStart = xlsread(tmpname, 'Summary', 'C8');
        CNTCrashWindowEnd = xlsread(tmpname, 'Summary', 'D8');
        subjectNum = xlsread(tmpname, 'User Info', 'A2');

        eeglab;
        %eeglab things happen here

    catch ME
        disp(ME)
    end
end 
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 1
    As a general rule I try to avoid manual concatenation of folders/files and rather favor [`fullfile`](http://www.mathworks.com/help/matlab/ref/fullfile.html) (e.g. `tempname = fullfile(folder, files(ii).name);`). It ensures the file separators are all present and correct for the filesystem, so it's generally more robust. – sco1 Dec 15 '15 at 14:33