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: Ron.Simonson
Subject: Re: file where sep decimals is comma
Date: Fri, 23 Sep 2011 11:31:18 -0700
User-agent: Thunderbird 2.0.0.24 (X11/20100228)

Ron.Simonson wrote:
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

sed -e "s/,/./" -e "s/;/,/" old_file > new_file


sed 's/,/./; s/;/,/' old > new

does the trick with less writing, although it does look like my cat
danced on the keyboard.

--
Here is an octave-only solution:

function X = csvread_ns(file)
 fid=fopen(file);
  X=[];
  while ((line=fgetl(fid)) && !(line == -1))
   line=strrep(line,",",".");
   line=strrep(line,";",",");
   x=sscanf(line,"%i,%f");
   X=[X,x];
 endwhile
 fclose(fid);
endfunction
octave> > > > > > > > > > > octave> system("cat /tmp/octave.txt");
1953;1453,6
1954;1480,1
1955;1576,4
1956;1621,5
1983;4036,7
1984;4249,6
octave> csvread_ns("/tmp/octave.txt")
ans =

   1953.0   1954.0   1955.0   1956.0   1983.0   1984.0
   1453.6   1480.1   1576.4   1621.5   4036.7   4249.6

_______________________________________________
Help-octave mailing list
address@hidden
https://mailman.cae.wisc.edu/listinfo/help-octave

Does
indata = dlmread("data_filename", ";");
work?

Talk to you later.  Ron.

_______________________________________________

Sorry, I missed the decimal part,
system("sed 's/,/./g' datafile_name > tempfile");
indata = dlmread("tempfile", ";");
system("rm tempfile");

Talk to you later. Ron.




reply via email to

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