MATLAB:如何导入具有混合数据类型的多个CSV文件 [英] MATLAB: How to import multiple CSV files with mixed data types

查看:220
本文介绍了MATLAB:如何导入具有混合数据类型的多个CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我需要的样例csv:(all csv文件的格式与固定的列相同)

$ p $ 日期,代码,数字...
2012 / 1/1,00020.x1,10
2012/1/2,00203.x1,0300
...


$ b $ csvread()仅适用于整数,我应该分别导入数字数据还是文本数据,还是有快捷方式使用混合数据类型导入多个csv 文件



非常感谢! b

解决方案

您正在寻找的可能是 xlsread

<打开任何由Excel识别的文件,并自动将文本数据与数字数据分开。



问题在于le的默认分隔符在我的电脑上是; ,而不是(至少对于我在巴西的语言环境)。因此,xlsread会尝试使用; 来分隔文件中的字段,而不是逗号。



要更改您必须更改您的系统区域设置以将逗号添加为列表分隔符。所以,如果你觉得这样做,要在Windows Vista中,单击开始,控制面板,区域和语言选项,自定义此格式,并将列表分隔符从';'更改为','。在其他窗口上,这个过程应该几乎是一样的。



完成后,输入:

  [num,txt,all] = xlsread('your_file.csv'); 

会返回类似于:

  num = 

10
300


txt =

'01/01 / 2012''00020.x1'
'02 / 01/2012''00203.x1'


全部=

'01 / 01 / 2012''00020.x1'[10]
'02 / 01/2012''00203.x1'[300]

请注意,如果您的语言环境已将列表分隔符设置为,,则不必在系统上更改任何内容即可完成此工作。



如果您不想仅使用 xlsread 函数来更改系统,则可以使用此处描述的 textscan 函数: http://www.mathworks.com/help/techdoc/ref/textscan.html



问题在于它不像调用它那么简单,因为您将不得不打开文件,迭代行,并且te我们会明确地表示你的文件的格式。



最好的祝愿


I have just started learning MATLAB and have difficulties to import csv files to a 2-D array..

Here is a sample csv for my needs:(all the csv files are in the same format with fixed columns)

Date,                Code,         Number....
2012/1/1,            00020.x1,             10
2012/1/2,            00203.x1,            0300
...

As csvread() only works with integer numbers, should I import numeric data and text data separately or is there any quick way to import multiple csv files with mixed data types?

Thanks a lot!!

解决方案

What you're looking for is maybe the function xlsread.

It opens any file recognized by Excel, and automatically separates text data from numerical data.

The problem is that the default delimiter for at least on my computer is ;, and not , (at least for my locale here in Brazil). So xlsread will try to separate the fields on the file with a ;, and not a comma as you'd like.

To change that you have to change your system locales to add the comma as the list separator. So if you feel like it, to do it in windows vista, click Start, Control Panel, Regional and Language Options, Customize this format, and change the List Separator from ';' to ','. On other windows the process should be almost the same.

After doing that, typing:

[num, txt, all] = xlsread('your_file.csv');

will return something like:

num =

10
300


txt = 

'01/01/2012'    ' 00020.x1'
'02/01/2012'    ' 00203.x1'


all = 

'01/01/2012'    ' 00020.x1'    [ 10]
'02/01/2012'    ' 00203.x1'    [300]

Notice that if your locale has already the list separator set to ',', you won't have to change anything on your system to make that work.

If you don't want to change your system just to use the xlsread function, then you could use the textscan function described here: http://www.mathworks.com/help/techdoc/ref/textscan.html

The problem is that it is not as simple as calling it, as you will have to open the file, iterate on the lines, and tell matlab explicitly the format of your file.

Best regards

这篇关于MATLAB:如何导入具有混合数据类型的多个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆