问题
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
my current code looks like that:
hist_data_f = fopen(hist_path)
fgets(hist_data_f);
hist_data = textscan(hist_data_f, '%s,%d,%d,%d,%d,%d,%d')
the current result is this:
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
... and so on.
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
...
]
other tries i've done:
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?
ive read through Reading CSV files with MATLAB? Reading .csv file into MATLAB and some others without luck.
回答1:
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);
the out put is then as required:
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
回答2:
I don't know how are the , in the format specifier interpreted. I think you are looking for:
textscan(str,'%s%d%d%d%d%d','Delimiter',',')
回答3:
Sebastien - if all you want is a cell array of strings in your hist_data then why not continue as before, but just treat every line as if it were a string instead of trying to determine which are strings and which are numbers? Something like
hist_data_f = fopen(hist_path)
fgets(hist_data_f);
hist_data = textscan(hist_data_f, '%s\n'); % now every line is a string only
hist_data is a cell array, and so now just replace all commas with four (or more) spaces
hist_data = strrep(hist_data{1},',',' ');
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'
'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'
Try the above and see what happens!
来源:https://stackoverflow.com/questions/25462433/read-csv-data-to-matlab