help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Is it possible to load date values from CSV file?


From: Tim Rueth
Subject: RE: Is it possible to load date values from CSV file?
Date: Mon, 20 Dec 2010 05:46:06 -0800

> -----Original Message-----
> From: CdeMills [mailto:address@hidden 
> Sent: Monday, December 20, 2010 1:16 AM
> To: address@hidden
> Subject: Re: Is it possible to load date values from CSV file?
> 
> 
> 
> Leonid Krashenko wrote:
> > 
> > For example, CSV's content is:
> > 
> > 10-Dec-2010, 12
> > 17-Dec-2010, 17
> > ...
> > 
> > need something like:
> > 
> > values = load_from_csv(filename);
> > 
> It depends on the file complexity. If it is totally regular, 
> you have base functions "dlmread". Then, in octave-forge, you 
> have package io: csvread package miscellanous: csv2cell 
> package dataframe: x=dataframe(the_file)
> 
> I'm the author of the last solution, and I wrote it because I 
> had to analyse CSV files with variable number of fields by 
> lines and other oddities.
> 
> Regards
> 
> Pascal

Interesting.  I didn't think dlmread() would work for strings.  I currently
use textread() to read in date-formated data, which doesn't appear to be
very fast.  But perhaps calling datenum(char...) is what is slowing it down.
I didn't know there was csv2cell().  Anyway, here's a simplified version of
my present code using textread(), which works:

function [date_v data_v] = read_date_data(infile);
        [date_cstr, data_v] = textread(infile, '%s %f');
        date_v = single(datenum(char(date_cstr{:})));
endfunction;

So, I tried csv2cell() so I could compare speeds:

function [date_v data_v] = read_date_data(infile);
        [cell_array] = csv2cell(infile, ' ');   # space-delimited data
        date_v = single(datenum(char(cell_array{:,1})));
        data_v = cell_array{:,2};                       # this isn't
right...how do I fix this?
endfunction;

But this code doesn't quite work, because I need to convert the cell data in
column 2 to numbers, like a "cell2num" function which I couldn't find.

Question #1:  How do I assign numbers stored as cells in column 2 to just a
normal number array?
Question #2:  It looks like it's the datenum(char... part that is really
slowing things down.  Is there a faster way to read in date strings and
convert them to datenums?

Thanks,

--Tim



reply via email to

[Prev in Thread] Current Thread [Next in Thread]