Why do I receive an empty array when I read a CSV file with XLSREAD in MATLAB 7.10 (R2010a)?

8 vues (au cours des 30 derniers jours)
I have some data in a CSV file (comma separated values) and I am reading it into MATLAB using the following code:
data = xlsread('name_of_file.csv');
[m, n] = size(data);
The output I receive is an empty matrix with 'm=0' and 'n=0'. Why is this happening when my file is not empty?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 12 Sep 2013
The reason why an empty array is generated when reading a CSV file using XLSREAD in the following way:
data = xlsread('name_of_file.csv');
might be due to the language and regional settings on the computer.
If this is the case, it can be visualised by reading the data in the following way:
[data, text] = xlsread('name_of_file.csv');
Then, all the numeric data values will be written as text in the variable 'text'.
In some regional settings the commas are used to mark the decimal positions in numbers whereas in CSV files they are used to separate the data values. In those regional settings XLSREAD is not able to recognize numbers and reads all the data as text.
In order to avoid this issue, a possible workaround is to change the language settings to English. The language/regional settings can be accessed via the 'Control Panel' on a Windows machine.
If changing the regional settings is not an option, it is possible to import the data from the CSV file directly using the Import Wizard by clicking on 'File'->'Import Data'. When the Import Wizard window opens, there is a check box that says 'Generate MATLAB Code' at the bottom of the window. By clicking on it, some MATLAB code will be generated that can be run to import CSV files automatically. Then, by saving the generated function as, for example, IMPORTDATA, it will be possible to import CSV files automatically without having to click on 'File'->'Import Data' by typing
importdata('name_of_file.csv');

Plus de réponses (0)

Tags

Produits


Version

R2010a

Community Treasure Hunt

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

Start Hunting!

Translated by