load columns from file with headlines and dates

4 次查看(过去 30 天)
date, Value(1) , Value(2) , Value(3) , Value(4) ........,Value(200)
date, ?, ?, ?, ?
2010/10/01 00:00:00, 0 , 0 , 0 , 0 ,....
2010/10/01 01:00:00, 12, 5.4, 0 , 0 ,....
2010/10/01 02:00:00, 7.6, 3.99, 1.18, 0 ,...
2010/10/01 03:00:00, 4.06, 0.0008, 4.05, 6.84,...
2010/10/01 04:00:00, 1.44, 0.0020, 4.096, 8.14,...
Hi!
I have a file like the one above (with headers and dates on the first column). With few columns I'm opening it with importfile('myfile.csv');
However I want to load only from, for example, column 100 to column 101. Any suggestions? I was trying with: dlmread('myfile',',','headlines',2) but doesnt work
Thanks

回答(2 个)

Chandrasekhar
Chandrasekhar 2013-4-23
it can be read by using csvread command of matlab
  2 个评论
Ana
Ana 2013-4-23
It's not working. the file has headlines and the first column of the files are dates...
Matt Kindig
Matt Kindig 2013-4-23
编辑:Matt Kindig 2013-4-23
I think this should work using textread():
Format = ['%*s %*s ', repmat('%*f', [1 98]), '%f %f', '%*[\n]'];
data = textread('/path/to/your/file.csv', Format, 'delimiter', ',', 'headerLines', 2);

请先登录,再进行评论。


Cedric
Cedric 2013-4-23
编辑:Cedric 2013-4-23
Assuming that the date/time column has always the same format, the first value starts at char 22 and you can do something like:
cols = [100, 101] ;
buffer = zeros(1e6, length(cols)) ; % Prealloc for 1e6 entries.
fid = fopen('myFile.csv', 'r') ;
lCnt = 0 ;
while (~feof(fid))
line = fgetl(fid) ;
if line(1) == 'd', continue ; end % Skip lines starting with 'd'.
lCnt = lCnt + 1 ;
values = textscan(line(22:end), '%f,', Inf) ;
buffer(lCnt,:) = values{1}(cols) ;
end
fclose(fid) ;
buffer = buffer(1:lCnt,:) ; % Truncate to number of records.
Note that you might want to realloc buffer when lCnt hits 1e6 (or whichever value you are using for the prealloc). Also, a TEXTREAD guru could probably do this in one shot using this function.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by