Quantcast

Data import from octave / text file

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Data import from octave / text file

kaffeepause73
Dear all,

I want to read numeric data in vector / matrix format generated by octave. As I haven't seen any octave specific libraries yet (only matlab), I'm tyrying the way via ascii - file.

The folloing file log.txt contains three signal traces in the three columns (time, signal 1, signal 2)

 0 30 9
 0.1 30 9
 0.2 30 9
 0.3 30 9
 0.4 30 9
 0.5 30 9
 0.6000000000000001 30 9
 0.7000000000000001 30 9
 0.8 30 9
 0.9 30 9
 1 30 9

I got the following bit of code compiled with ghc:

import System.IO

main = do
     text <- readFile "./log.txt"  
     putStrLn text    
     
     putStrLn "SecondLot"
     
     let m = read text :: [[Double]]
     putStrLn (show m)
     
     putStrLn "Finish"

Reading text works, but not reading text into m. The Output:

 0 30 9
 0.1 30 9
 0.2 30 9
 0.3 30 9
 0.4 30 9
 0.5 30 9
 0.6000000000000001 30 9
 0.7000000000000001 30 9
 0.8 30 9
 0.9 30 9
 1 30 9

SecondLot
signalImport: Prelude.read: no parse

Any suggestions ? - Thanks in advance.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data import from octave / text file

Claude Heiland-Allen-2
Hi,

On 15/06/11 12:13, kaffeepause73 wrote:
>       let m = read text :: [[Double]]
> signalImport: Prelude.read: no parse

read :: String -> [[Double]] -- expects Haskell syntax

try something like:

parse :: String -> [[Double]] -- expects plainer syntax
parse = map (map read . words) . lines


Claude
--
http://claudiusmaximus.goto10.org

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data import from octave / text file

Lars Viklund
In reply to this post by kaffeepause73
On Wed, Jun 15, 2011 at 04:13:49AM -0700, kaffeepause73 wrote:

> The folloing file log.txt contains three signal traces in the three columns
> (time, signal 1, signal 2)
>
>  0 30 9
>  0.1 30 9
>  0.2 30 9
>
>      let m = read text :: [[Double]]
>
> SecondLot
> signalImport: Prelude.read: no parse

Instances of Read tends to expect the format that Show would output. In
the case of lists, it's [elem1,elem2,elem3].

You can either modify your exporter to output values in the format that
read expects, or you could use a combination of the functions 'lines',
'words' and 'read' to split the input into a list of lines, split each
line into a list of words, and finally use read to go from word to
Double.

--
Lars Viklund | [hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data import from octave / text file

Victor Gorokgov
In reply to this post by kaffeepause73
read :: String ->  [[Double]] can't read this format. It expects "[[0,30,9],[0.1,30,9],...]"

use this code
let m = map f text :: [[Double]]
     f a = map g $ lines a
     g a = map read $ words a


15.06.2011 15:13, kaffeepause73 пишет:

> Dear all,
>
> I want to read numeric data in vector / matrix format generated by octave.
> As I haven't seen any octave specific libraries yet (only matlab), I'm
> tyrying the way via ascii - file.
>
> The folloing file log.txt contains three signal traces in the three columns
> (time, signal 1, signal 2)
>
>   0 30 9
>   0.1 30 9
>   0.2 30 9
>   0.3 30 9
>   0.4 30 9
>   0.5 30 9
>   0.6000000000000001 30 9
>   0.7000000000000001 30 9
>   0.8 30 9
>   0.9 30 9
>   1 30 9
>
> I got the following bit of code compiled with ghc:
>
> import System.IO
>
> main = do
>       text<- readFile "./log.txt"
>       putStrLn text
>
>       putStrLn "SecondLot"
>
>       let m = read text :: [[Double]]
>       putStrLn (show m)
>
>       putStrLn "Finish"
>
> Reading text works, but not reading text into m. The Output:
>
>   0 30 9
>   0.1 30 9
>   0.2 30 9
>   0.3 30 9
>   0.4 30 9
>   0.5 30 9
>   0.6000000000000001 30 9
>   0.7000000000000001 30 9
>   0.8 30 9
>   0.9 30 9
>   1 30 9
>
> SecondLot
> signalImport: Prelude.read: no parse
>
> Any suggestions ? - Thanks in advance.
>
> --
> View this message in context: http://haskell.1045720.n5.nabble.com/Data-import-from-octave-text-file-tp4490870p4490870.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data import from octave / text file

kaffeepause73
In reply to this post by kaffeepause73
Hey guys,

thanks for the very quick help. The code works and looks like:
(any tips regards speed and memory usage ?)  
import System.IO
import Data.Array
import Data.Packed.Matrix

parse :: String -> [[Double]] -- expects plainer syntax
parse = map (map read . words) . lines



main = do
     -- read and show plain text
     text <- readFile "./log.txt"
     putStrLn text
     
     -- read and show parsed list of lists
     putStrLn "parsed list of lists"
     let m = parse text
     putStrLn (show m)
     
     -- convert to matrix and show
     let a = fromLists m    
     putStrLn (show a)
     
     -- cuts out first row to get signal
     let t = takeColumns 1 a
     putStrLn (show t)

     putStrLn "Finish"

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data import from octave / text file

Philipp Schuster
Hi,

try Numeric.Container.loadMatrix.

Regards,
Philipp


On 15.06.2011 14:59, kaffeepause73 wrote:

> Hey guys,
>
> thanks for the very quick help. The code works and looks like:
> (any tips regards speed and memory usage ?)
> import System.IO
> import Data.Array
> import Data.Packed.Matrix
>
> parse :: String ->  [[Double]] -- expects plainer syntax
> parse = map (map read . words) . lines
>
>
>
> main = do
>       -- read and show plain text
>       text<- readFile "./log.txt"
>       putStrLn text
>
>       -- read and show parsed list of lists
>       putStrLn "parsed list of lists"
>       let m = parse text
>       putStrLn (show m)
>
>       -- convert to matrix and show
>       let a = fromLists m
>       putStrLn (show a)
>
>       -- cuts out first row to get signal
>       let t = takeColumns 1 a
>       putStrLn (show t)
>
>       putStrLn "Finish"
>
>
>
> --
> View this message in context: http://haskell.1045720.n5.nabble.com/Data-import-from-octave-text-file-tp4490870p4491136.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Loading...