2

I have two computers with the same MATLAB code and same Excel file (.csv format). The code only works on the machine that has Excel. I can't think of any other differences.

Does a computer need Excel for xlsread to work? The error I'm getting is an unrecognized format.

If this is the case, are there any easy workarounds without getting Excel?

EDIT: It appears that Excel is not needed. Maybe the issue is that the file is a .csv? It is a format error after all. I just can't imagine why a file of the same format worked on my other computer.

SOLVED: The .csv file was the problem. For reading .csv files, the importdata() function of matlab proved to be really versatile.

Community
  • 1
  • 1
Dan
  • 474
  • 1
  • 7
  • 16
  • 1
    A `csv` file is not, repeat **NOT** an Excel file. It is an ascii text file which happens to use a comma as a delimiter. Why are you using an Excel-reading function rather than any of the builtin text-file tools? – Carl Witthoft Jul 20 '15 at 14:29
  • PLease read the posting guidelines and supply your actual code that is failing to read the file **and** a small, reproducible sample text file. – Carl Witthoft Jul 20 '15 at 14:31

3 Answers3

6

According to this page

If your system has Excel® for Windows® installed, including the COM server (part of the typical installation of Excel):

All MATLAB® import options support XLS, XLSX, XLSB, XLSM, XLTM, and XLTX formats.

...

If your system does not have Excel for Windows installed, or the COM server is not available:

All MATLAB import options read XLS, XLSX, XLSM, XLTM, and XLTX files.

However, if you are just trying to import a comma delimited ASCII file, then xlsread is way overkill and super slow. If your data is purely numeric, use csvread or dlmread. If your data is mixed, then use textscan instead.

Community
  • 1
  • 1
craigim
  • 3,884
  • 1
  • 22
  • 42
3

xlsread(filename,sheet,xlRange,'basic') will work without Excel installed on your machine.

Other additional arguments will require Excel to be installed.

Matlab can import character separated files natively though.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • The file name is the only argument that I'm using. – Dan Jul 20 '15 at 14:02
  • Bung in the other ones in just to test. Perhaps the defaulting is different. But do try to parse the csv natively though. – Bathsheba Jul 20 '15 at 14:03
  • I've tested with just the file name, the file name and the sheet, and also all four arguments. All return the same error. The same is true for when I try something like dlmread or csvread. I'm starting to think that maybe this is a file issue or my matlab is lacking some sort of functionality. – Dan Jul 20 '15 at 14:06
  • To eliminate one possibility, could you use the excel you do have to convert your csv to a native xls, and try that? – Bathsheba Jul 20 '15 at 14:07
  • Wow, good call. It totally works when I save it as a .xlsx. With that being said, I actually made a mistake in my previous comment. csvread gives a different error, saying it has trouble reading the Numeric field in the first cell. – Dan Jul 20 '15 at 14:21
  • 1
    Your CSV error is probably because you have mixed data (numeric along with date and/or text). See [here](http://stackoverflow.com/questions/19613232/reading-csv-files-with-matlab/19613301#19613301) for more details. – craigim Jul 20 '15 at 14:30
2

I have never used xlsread on a computer without Excel, so I can't be sure. But according to the documentation Excel is not necessary; you only lose some functionality:

num = xlsread(filename,sheet,xlRange,'basic') reads data from the spreadsheet in basic import mode. If your computer does not have Excel for Windows®, xlsread automatically operates in basic import mode, which supports XLS, XLSX, XLSM, XLTX, and XLTM files.

basic mode is the default for computers without Excel for Windows. In basic mode, xlsread:

  • Reads XLS, XLSX, XLSM, XLTX, and XLTM files only.

  • Does not support an xlRange input when reading XLS files. In this case, use '' in place of xlRange.

  • Does not support function handle inputs.

  • Imports all dates as Excel serial date numbers. Excel serial date numbers use a different reference date than MATLAB® date numbers.

So, perhaps the problem is that you are calling xlsread with some options that are not supported in basic mode.

Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • The file name is the only argument. I have also tried adding in some of the other arguments, using csvread, dlmread, etc. None of it has solved the issue. – Dan Jul 20 '15 at 13:53