读取csv数据到matlab [英] Read csv data to matlab

查看:259
本文介绍了读取csv数据到matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有解析csv文件的问题:

i have issues parsing a csv file:

Date,Open,High,Low,Close,Volume,Adj Close
2014-08-22,16.08,16.19,15.80,16.00,139800,16.00
2014-08-21,16.16,16.33,16.00,16.10,128500,16.10
2014-08-20,16.00,16.28,15.78,16.15,271200,16.15
2014-08-19,16.20,16.27,15.96,16.04,379600,16.04
2014-08-18,16.87,16.87,16.10,16.16,259200,16.16
2014-08-15,16.54,16.55,16.21,16.38,190900,16.38

我当前的代码如下:

hist_data_f = fopen(hist_path)
fgets(hist_data_f);
hist_data = textscan(hist_data_f, '%s,%d,%d,%d,%d,%d,%d')

当前结果是:

hist_data =
{
  [1,1] =
  {
    [1,1] = 2014-08-22,16.08,16.19,15.80,16.00,139800,16.00
    [2,1] = 2014-08-21
    [3,1] = ,
    [4,1] = 16.20
    [5,1] = ,
    [6,1] = 16.55
    [7,1] = ,
    [8,1] = 16.41
    [9,1] = ,
    [10,1] = 16.53
    [11,1] = ,
    [12,1] = 327700,16.13
    [13,1] = 2014-08-05,17.00,17.29,16.91,17.01,125500,16.67
    [14,1] = 2014-08-04
    [15,1] = ,
    [16,1] = 17.86
    [17,1] = ,
    [18,1] = 18.80
    [19,1] = ,
    [20,1] = 18.96
    [21,1] = ,
    [22,1] = 19.10

...等等。

my目标是获得如下数据:

my goal is to have the data like this:

hist_data = 
    [ 
      2014-08-22    16.08    16.19    15.80    16.00    139800    16.00
      2014-08-21    16.16    16.33    16.00    16.10    128500    16.10
      ...
    ]

我做过的其他尝试:

hist_data = textscan(hist_data_f, '%s,%d,%d,%d,%d,%d,%d','Delimiter',',')
hist_data = csvread('hist_data.csv')

我似乎没有得到所需的输出。
任何建议?

i cant seem to get the desired output. any suggestions?

通读
使用MATLAB读取CSV文件
阅读.csv文件到MATLAB
和其他一些没有运气。

ive read through Reading CSV files with MATLAB? Reading .csv file into MATLAB and some others without luck.

推荐答案

=http://www.mathworks.co.uk/help/matlab/ref/csvread.html =nofollow> csv read 数字部分

How about: csv read for the numeric part

%reads the csv file from row 1 on, to avoid the headers, and string date
M = csvread('test.csv',1,1);

然后根据需要输出:

 M =

   1.0e+05 *

    0.0002    0.0002    0.0002    0.0002    1.3980    0.0002
    0.0002    0.0002    0.0002    0.0002    1.2850    0.0002
    0.0002    0.0002    0.0002    0.0002    2.7120    0.0002
    0.0002    0.0002    0.0002    0.0002    3.7960    0.0002
    0.0002    0.0002    0.0002    0.0002    2.5920    0.0002
    0.0002    0.0002    0.0002    0.0002    1.9090    0.0002

这篇关于读取csv数据到matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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