help-octave
[Top][All Lists]
Advanced

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

Re: CSV Import


From: Markus Bergholz
Subject: Re: CSV Import
Date: Mon, 28 Jan 2013 09:34:20 +0100



Hello Rich

On Mon, Jan 28, 2013 at 12:30 AM, ramack <address@hidden> wrote:
I realize this is an old thread that I'm resurrecting, but it is very similar
to what I am wanting to do. I need to load or import a text file with a
date/time stamp in the first column, header info is in the first row.

The partial contents of the file.csv are:
"Date","Time","Open","High","Low","Close","Total Ticks"
1/26/2012 5:00,1.06293,1.06688,1.05914,1.06507,96345

Using dlmread("file.csv",",")
0 0 0 0 0 0 0
1 1.06293 1.06688 1.05914 1.06507 96345 0

I'm not sure what I need to do in order to retain the date/time stamp.  How
can the first column be imported as a character string while the remaining
columns are numeric.
Rich


For import something as a string, you have to use textscan imho.
First i would read in all numeric stuff like that
yournumericvalues=dlmread("file.csv",",",1,1); %To skip row 1 and column 1.

Now you have to open the file with fopen
fid = fopen("file.csv",'r'); 
r_rows =  sprintf('%s %s', '%s' ,repmat('%f ',1,rows(yournumericalvalues)));
data = "" r_rows, 'Delimiter', ',',  'HeaderLines', 12);
fclose(fid);
 
now you'll find in the cell data your first date column as a string, and the other values are inside the cell too.
for calculating/plot with the date string, you have to convert the datestring in a serial number

i would outsource this in a separate function like that

function [d] = yourdateaction(x)
		d =  datenum(x,'DD/MM/YYYY') ;
to get the date back
datestr(ans,'DD:MM:YYYY')
ans = 05:08:1991
for example...
you can call this function with cellfun

yourfinalvalue(:,1) = cellfun(@yourdateaction, data{1});
yourfinelvalue(:,2:(rows(yournumericalvalues)+1)) = yournumericalvalues;



i hope that helps. mabe you have to edit your datenum for your needs.
 



--
View this message in context: http://octave.1599824.n4.nabble.com/CSV-Import-tp2300503p4649173.html
Sent from the Octave - General mailing list archive at Nabble.com.
_______________________________________________
Help-octave mailing list
address@hidden
https://mailman.cae.wisc.edu/listinfo/help-octave



--
icq: 167498924
XMPP|Jabber: address@hidden

reply via email to

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