Why do I receive an empty array when I read a CSV file with XLSREAD in MATLAB 7.10 (R2010a)?
3 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
MathWorks Support Team
2013-9-12
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');
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!