help-octave
[Top][All Lists]
Advanced

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

Re: file where sep decimals is comma


From: Bård Skaflestad
Subject: Re: file where sep decimals is comma
Date: Fri, 23 Sep 2011 20:04:30 +0200

On Fri, 2011-09-23 at 18:52 +0200, Leo Butler wrote:
> Marco Atzeri <address@hidden> writes:
> 
> > On 9/23/2011 3:13 PM, Sebastian Kruk wrote:
> >> Hi, I have a csv file where sep is ";" and I have "," as decimal point.
> >>
> >> How I can import directly in Octave?
> >>
> >> An example:
> >>
> >> 1953;1453,6
> >> 1954;1480,1
> >> 1955;1576,4
> >> 1956;1621,5
> > [cut]
> >> 1983;4036,7
> >> 1984;4249,6
> >>
> >> Thanks,
> >>
> >> Sebastián.
> >
> > sed is your friend, in two step

If you have the memory resources you can even implement a fairly
efficient pure Octave solution.  The following is probably too naive,
but may be adapted to suite you needs.

        function v = csvread_ns(file, nfields)
           [fid, msg] = fopen(file, "r");
           if fid < 0,
              error("Unable to open '%s': %s", file, msg);
           end
        
           t = reshape(fread(fid, "*char"), 1, []);
           t = regexprep(t, {";", ","}, {" ", "."});
           v = reshape(sscanf(t{1}, "%f"), nfields, []) .';
        
           fclose(fid);
        endfunction


Sincerely,
-- 
Bård Skaflestad <address@hidden>
SINTEF ICT, Applied Mathematics



reply via email to

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