Matlab can't find files using xlsread

Hi,
I'm using a MATLAB script to compile a massive amount of .csv files into one. The script works fine and has run successfully before, but I am having major problems with xlsread returning error messages similar to the following. (I replaced the foldername and filename because they are a little long and also contain some sensitive info)
XLSREAD unable to open file
'C:\Output\foldername\filename.csv'
File
'C:\Output\foldername\filename.csv'
not found.
I have tried adding the folder to the MATLAB path, but afterward I still get this message and also get error messages using excel with a similar file not found. I was wondering if it was a MATLAB problem or another issue.
Using the exist function before returns a nonzero number, but after adding to the path returns 0. Additionally, moving the files to a different directory for some reason allows excel to open the files, but still not matlab.
I am running MATLAB R2014a on a Windows XP Profesional Version 2002 Service Pack 3 using Excel 2010.
EDIT: It turns out there is actually a maximum allowable pathlength in windows, which is around 260. I found 220, but that just might be my version or system.

3 Comments

I'm not sure why, but shortening the filenames/directory is resolving the issue. There might be a limit on the amount of characters in a filename/directory, which was around 250 characters (not by design).
If anyone could suggest why, that would be great.
Excel has a path length limit of 218 characters: http://support.microsoft.com/kb/213983
John Lutz
John Lutz on 12 Jun 2017
Edited: John Lutz on 12 Jun 2017
Just ran into this issue and resolved it in a very strange way. Moved the .m script file and the excel spread sheet to a different directory and tried to run the script. matlab said something about path and i added the path. then i added both files back to the original directory and added the path again and it worked.

Sign in to comment.

 Accepted Answer

Excel has nothing to do with it. If exist() says the file is not there, then nothing (including Excel) will be able to find it either. Can't really say much more unless more specifics are given.
You certainly don't want to use Excel anyway to read in "massive" numbers of csv files and combine them into one. It will have to launch and shutdown Excel each time and that will take forever . Your best bet is to simply open them up as text files with fopen() and get lines with fgetl() and transfer them to the single output file with fprintf(). Finally close the files with fclose().

3 Comments

Allen
Allen on 27 Jun 2014
Edited: Walter Roberson on 13 Oct 2015
Thanks for responding!
Speed is not really my issue here. Before all of the file not found issues, it ran fairly well (not optimally, but within a reasonable amount of time given the amount of data). My main specification for this project is accuracy of data processing and robustness. xlsread is also easier for people without much experience with matlab to understand, which is who I am developing for.
It's really strange because I know the file exists and moving it around confirms it isn't corrupted, but whenever it's moved into a MATLAB related path it just disappears.
It maybe how you're calling the file. To make things very simple. create a very simple folder directory like 'C:\temp' and place one of the csv files there. then using I.A's suggestion try opening it with fopen(). if it works then there is something in how you're defining where that file is.
example:
fid = fopen('C:\temp\randomfile.csv')
testline = fgetl(fid)
fclose(fid);
or use uigetfile to navigate and get the path of the file.
[filename pathname]=uigetfile('*.csv','Pick CSV file')
fid = fopen([pathname filename])
testline = fgetl(fid)
fclose(fid);
Hi Joseph, Thank you! I have actually tried doing that, but with no luck on opening the file either.
I think the issue is actually the length of the file name, most likely with excel, not with matlab. I have tested this by changing the file name, with the same directory and everything else to under 220 characters (Including the full path). It's a working theory, and might not be true, but it seems to have worked.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 27 Jun 2014

Edited:

on 12 Jun 2017

Community Treasure Hunt

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

Start Hunting!