Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading text files in Scilab, dates and Excel

2,691 views
Skip to first unread message

Roederick

unread,
Nov 4, 2004, 10:17:33 AM11/4/04
to
Hi guys!

Whilst trying to move from MatLab to Scilab I have ecountered another
hands on-related problem... (I am not a trained programmer so I guess
that makes the transition a bit more difficult! :)

What I need to do is to read a tab-separated (data) text file into
Scilab. This is 'conveniently' done in MatLab with the 'textread'
command. I have been fooling around in Scilab a bit using
predominantly the 'excel2sci'and 'read' commands without quite getting
the hang of it. I did also search this newsgroup without really
finding an appropriate thread, so...

The text file looks as follows:

3.11.2004 10:01:07 0 41.3 86.4 50 ...
3.11.2004 10:01:07 0 41.265 86.47 50.045
3.11.2004 10:02:11 0 41.28 86.63 50.03
3.11.2004 10:03:13 111.75 41.31 86.74 50.075
3.11.2004 10:04:16 112.15 41.13 87.11 50.035
3.11.2004 10:05:19 111.76 41.065 86.905 49.98
3.11.2004 10:06:02 111.83 41.07 86.81 49.9
3.11.2004 10:07:05 111.92 41.09 86.905 49.95
3.11.2004 10:08:10 111.78 41.085 86.935 49.97
3.11.2004 10:09:13 110.595 41.13 86.855 49.91
3.11.2004 10:10:16 110.075 41.09 86.865 49.905
3.11.2004 10:11:19 109.96 41.085 86.715 49.895
.
.
.

How do I read this file into a single matrix in Scilab so that the
first column consists of the timestamps and the rest of the columns of
each respective priceseries?

Further, how does Scilab handle dates? Any functions available similar
to MatLab's 'datenum' and 'datestr' funtions? In other words, how do I
compare timestamps to one another and/or convert them? I have
understood that there is no other way of transporting results
(vectors, matrices) to Excel from Scilab than saving the stuff in a
csv-file first? Is this correct?

Grateful for any help received... I'm sort of stuck here!

regs,

R

Vinicius Lobosco

unread,
Nov 4, 2004, 10:34:51 AM11/4/04
to

Tervekuloa! Welcome to Scilab!

I usually use "load" to get the data in Scilab environment. I never
check the details if I'm obliged to...

Cheers, Vinicius

Roederick

unread,
Nov 5, 2004, 3:52:03 AM11/5/04
to
Vinicius Lobosco <vinicius...@paperplat.com> wrote in message news:<WZrid.11918$1p....@nntpserver.swip.net>...


Terve, terve.

Eh, well... since I'm trying to load a textfile the command 'load'
appears to be the wrong one.. no?

I would really appreciate if someone could help me with this!

regs,

R

Gaspard

unread,
Nov 5, 2004, 5:33:03 AM11/5/04
to Roederick

On 5 Nov 2004, Roederick wrote:

> Vinicius Lobosco <vinicius...@paperplat.com> wrote in message news:<WZrid.11918$1p....@nntpserver.swip.net>...
> > Roederick wrote:
> > > Hi guys!
> > >
> > > Whilst trying to move from MatLab to Scilab I have ecountered another
> > > hands on-related problem... (I am not a trained programmer so I guess
> > > that makes the transition a bit more difficult! :)
> > >
> > > What I need to do is to read a tab-separated (data) text file into
> > > Scilab. This is 'conveniently' done in MatLab with the 'textread'

What does 'textread' do? Putting each line into a an element of a string matrix?
You can achieve that with the next function:

function stringvector=textread(filename)
stringvector = [];
fh = mopen(filename,'r');
while (~meof(fh)) do
stringvector = [stringvector; mgetl(fh,1)];
end
mclose(fh);
endfunction

(not tested,...)

But I assume that you want to convert is to numbers an putting them into a
matrix, so see below in this mail.

Ok, I had a similar problem some time ago (although without dates...).

Maybe fscanfMat can be usefull? But this function reads only numerical data.

For the your date-time problem: I guess the simplest solution would be to read
in your file in a spreadsheet and convert your date and time column to an epoch
number or a relative time. Save as text file and import in scilab.

Alternatively you can write a custom scilab function (I did that) that reads
line by line and converts the first two columns to an epoch. I did not find a
function in scilab that does this, but your shell should handle that (reading
`info date` can be usefull). Escape to it:

-->epoch_date=unix_g("date --date=''3.11.2004 10:01:07'' ''+%s''")

date: invalid date `3.11.2004 10:01:07'
epoch_date =


Gives an error, so rewrite it (look at the string functions in scilab):

-->epoch_date=unix_g("date --date=''3/11/2004 10:01:07'' ''+%s''")
epoch_date =

1078995667


CAVEAT: a string is returned!

To get the date back:

-->dt=getdate(evstr(epoch_date))
dt =

! 2004. 3. 11. 71. 5. 11. 10. 1. 7. 0. !

see `help getdate` for explanations.

But there seems to be some confussion: is '3' the month of the day?

Rewrite it in a unequivocal way: yyyy/mm/dd. The scilab function `tokens` will
be usefull for that.

(On a side note, I think getdate("datestring") should return the date structure
of the date specified in 'datestring' and not the current date...).


wfg,

Gaspard


Roederick

unread,
Nov 9, 2004, 9:56:11 AM11/9/04
to
Thank you, Gaspard!

I will have a look at your suggestions...

regs,

r


Gaspard <glequeux_at@ta_biomath.ugent.be> wrote in message news:<Pine.LNX.4.44.041105...@zeus.UGent.be>...

0 new messages