Thread Subject: Decimals from comma to dot

Subject: Decimals from comma to dot

From: Fiona Buckley

Date: 6 Jan, 2009 11:38:02

Message: 1 of 13

Hi,
I've got a file in ASCII format with decimal values comma seperated and columns are seperated by tab. The columns either have strings or floating numbers and my first column is in a ISO time-date format.
The file to be imported has column headers and titles which I don't need however, I do need all the string data and the floating number data from the general file.

I want to write a script to import this data, but as the floating points numbers are comma seperated I can't seem to manage. I previously wrote scripts to do this but up to now my decimals were always dot seperated. No matter what I do now I seem to be stuck. Here under some of the many functions I tried and what happened:
A) importdata - gives me a strucutre with one data field and one textdata field but almost all the strings have been trunckated and the numerical values are combined (i.e. 3,34 6,91 9,32 gives me 3 346 91932)
B) fopen - error message saying that the variable is undefined
C) textscan or textread - error message as the data is not recognised
D) strrep - I can't replace the string as I can't open the file

Does anyone have an idea???
Cheers
Fiona

Subject: Decimals from comma to dot

From: us

Date: 6 Jan, 2009 12:15:04

Message: 2 of 13

"Fiona Buckley"
> I want to write a script to import this data, but as the floating points numbers are comma seperated I can't seem to manage...

a hint:
other CSSMers have proposed a PERL script to do the conversion before using any one of the ML input procedures...
look for PERL in this NG...

us

Subject: Decimals from comma to dot

From: Vadim Teverovsky

Date: 6 Jan, 2009 13:05:30

Message: 3 of 13

Look up the help for the fopen command to see how to use that, then read in
the string (fileread), then use strrep, followed by textscan.


"Fiona Buckley" <buckley_fiona@yahoo.com> wrote in message
news:gjvfqq$muv$1@fred.mathworks.com...
> Hi,
> I've got a file in ASCII format with decimal values comma seperated and
> columns are seperated by tab. The columns either have strings or floating
> numbers and my first column is in a ISO time-date format.
> The file to be imported has column headers and titles which I don't need
> however, I do need all the string data and the floating number data from
> the general file.
>
> I want to write a script to import this data, but as the floating points
> numbers are comma seperated I can't seem to manage. I previously wrote
> scripts to do this but up to now my decimals were always dot seperated. No
> matter what I do now I seem to be stuck. Here under some of the many
> functions I tried and what happened:
> A) importdata - gives me a strucutre with one data field and one textdata
> field but almost all the strings have been trunckated and the numerical
> values are combined (i.e. 3,34 6,91 9,32 gives me 3 346 91932)
> B) fopen - error message saying that the variable is undefined
> C) textscan or textread - error message as the data is not recognised
> D) strrep - I can't replace the string as I can't open the file
>
> Does anyone have an idea???
> Cheers
> Fiona

Subject: Decimals from comma to dot

From: Fiona Buckley

Date: 7 Jan, 2009 10:08:01

Message: 4 of 13

"us " <us@neurol.unizh.ch> wrote in message <gjvi08$qab$1@fred.mathworks.com>...
> "Fiona Buckley"
> > I want to write a script to import this data, but as the floating points numbers are comma seperated I can't seem to manage...
>
> a hint:
> other CSSMers have proposed a PERL script to do the conversion before using any one of the ML input procedures...
> look for PERL in this NG...
>
> us



Thanks for the hint ;o)
I found the program and it is very slick, a real savious. However, I still have a problem... as my file is ASCII, I get the following message:
            ??? Error using ==> replaceinfile at 63
            Can't do inplace edit on Down: File exists.
However, if I save my ASCII file as a txt file I then can run the script with no problem at all. Any idea what I could do to avoid doing the save as manipulation?

Cheers
Fiona

Subject: Decimals from comma to dot

From: Pekka Kumpulainen

Date: 7 Jan, 2009 10:39:01

Message: 5 of 13

"Fiona Buckley" <buckley_fiona@yahoo.com> wrote in message <gk1uu1$8d7$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gjvi08$qab$1@fred.mathworks.com>...
> > "Fiona Buckley"
> > > I want to write a script to import this data, but as the floating points numbers are comma seperated I can't seem to manage...
> >
> > a hint:
> > other CSSMers have proposed a PERL script to do the conversion before using any one of the ML input procedures...
> > look for PERL in this NG...
> >
> > us
>
>
>
> Thanks for the hint ;o)
> I found the program and it is very slick, a real savious. However, I still have a problem... as my file is ASCII, I get the following message:
> ??? Error using ==> replaceinfile at 63
> Can't do inplace edit on Down: File exists.
> However, if I save my ASCII file as a txt file I then can run the script with no problem at all. Any idea what I could do to avoid doing the save as manipulation?
>
> Cheers
> Fiona

Hi, can you give a more detailed example of what you are doing?
How do you call replaceinfile, which file names you have etc?
You can also email me directly (Assuming you use the replaceinfile function I posted), I'll be happy to try to fix any problems.

Perl is not included in my native languages, sorry.. It was originally made to do exactly what you are trying to do.

Subject: Decimals from comma to dot

From: per isakson

Date: 7 Jan, 2009 18:52:01

Message: 6 of 13

"Fiona Buckley" <buckley_fiona@yahoo.com> wrote in message <gjvfqq$muv$1@fred.mathworks.com>...
> Hi,
> I've got a file in ASCII format with decimal values comma seperated and columns are seperated by tab. The columns either have strings or floating numbers and my first column is in a ISO time-date format.
> The file to be imported has column headers and titles which I don't need however, I do need all the string data and the floating number data from the general file.
>
> I want to write a script to import this data, but as the floating points numbers are comma seperated I can't seem to manage. I previously wrote scripts to do this but up to now my decimals were always dot seperated. No matter what I do now I seem to be stuck. Here under some of the many functions I tried and what happened:
> A) importdata - gives me a strucutre with one data field and one textdata field but almost all the strings have been trunckated and the numerical values are combined (i.e. 3,34 6,91 9,32 gives me 3 346 91932)
> B) fopen - error message saying that the variable is undefined
> C) textscan or textread - error message as the data is not recognised
> D) strrep - I can't replace the string as I can't open the file
>
> Does anyone have an idea???
> Cheers
> Fiona

One possibility is to run this function as a separate step. It doesn't work with comma separated files and it will replace commas in the header (if there is one)
/ per

function comma2point_overwrite( filespec )
% replaces all occurences of comma (",") with point (".") in a text-file.
% Note that the file is overwritten, which is the price for high speed.
    file = memmapfile( filespec, 'writable', true );
    comma = uint8(',');
    point = uint8('.');
    file.Data( transpose( file.Data==comma) ) = point;
    delete(file)
end

Subject: Decimals from comma to dot

From: Sdelarosa de la Rosa

Date: 4 Mar, 2009 16:20:17

Message: 7 of 13


> One possibility is to run this function as a separate step. It doesn't work with comma separated files and it will replace commas in the header (if there is one)
> / per
>
> function comma2point_overwrite( filespec )
> % replaces all occurences of comma (",") with point (".") in a text-file.
> % Note that the file is overwritten, which is the price for high speed.
> file = memmapfile( filespec, 'writable', true );
> comma = uint8(',');
> point = uint8('.');
> file.Data( transpose( file.Data==comma) ) = point;
> delete(file)
> end


Hi Per, thanks for this, I had exactly the same problem as Fiona. With this function I can now read in the .asc files without any problem using:

comma2point_overwrite( filespec )
[data] = importdata([filespec],'\t',headerlength);

You saved me (and surely others) from eternal hours of struggle! :-)

Subject: Decimals from comma to dot

From: Sdelarosa de la Rosa

Date: 4 Mar, 2009 16:20:18

Message: 8 of 13


> One possibility is to run this function as a separate step. It doesn't work with comma separated files and it will replace commas in the header (if there is one)
> / per
>
> function comma2point_overwrite( filespec )
> % replaces all occurences of comma (",") with point (".") in a text-file.
> % Note that the file is overwritten, which is the price for high speed.
> file = memmapfile( filespec, 'writable', true );
> comma = uint8(',');
> point = uint8('.');
> file.Data( transpose( file.Data==comma) ) = point;
> delete(file)
> end


Hi Per, thanks for this, I had exactly the same problem as Fiona. With this function I can now read in the .asc files without any problem using:

comma2point_overwrite( filespec )
[data] = importdata([filespec],'\t',headerlength);

You saved me (and surely others) from eternal hours of struggle! :-)

Subject: Decimals from comma to dot

From: Toby

Date: 21 Sep, 2009 10:18:02

Message: 9 of 13

just use notepad 2 (download http://www.flos-freeware.ch/notepad2.html)
it replaces commas with dots much faster an much simpler.

Subject: Decimals from comma to dot

From: Oleg Komarov

Date: 21 Sep, 2009 11:50:07

Message: 10 of 13

or simply in the international settings of your operating system, switch them to a comma-delimiting-decimal system. For example to the italian setting.

This method could be faster if u have to import many files and if they are big.

Oleg

Subject: Decimals from comma to dot

From: Diego

Date: 3 Nov, 2009 22:37:02

Message: 11 of 13

This script is great!!

Subject: Decimals from comma to dot

From: Laura

Date: 10 Nov, 2009 22:28:01

Message: 12 of 13

I also need to get rid of commas, but I'm hoping to find a solution that doesn't overwrite the original file. Does anyone know where I could find that?


"per isakson" <poi.nospam@bimDOTkthDOT.se> wrote in message <gk2tkh$64l$1@fred.mathworks.com>...
> "Fiona Buckley" <buckley_fiona@yahoo.com> wrote in message <gjvfqq$muv$1@fred.mathworks.com>...
> > Hi,
> > I've got a file in ASCII format with decimal values comma seperated and columns are seperated by tab. The columns either have strings or floating numbers and my first column is in a ISO time-date format.
> > The file to be imported has column headers and titles which I don't need however, I do need all the string data and the floating number data from the general file.
> >
> > I want to write a script to import this data, but as the floating points numbers are comma seperated I can't seem to manage. I previously wrote scripts to do this but up to now my decimals were always dot seperated. No matter what I do now I seem to be stuck. Here under some of the many functions I tried and what happened:
> > A) importdata - gives me a strucutre with one data field and one textdata field but almost all the strings have been trunckated and the numerical values are combined (i.e. 3,34 6,91 9,32 gives me 3 346 91932)
> > B) fopen - error message saying that the variable is undefined
> > C) textscan or textread - error message as the data is not recognised
> > D) strrep - I can't replace the string as I can't open the file
> >
> > Does anyone have an idea???
> > Cheers
> > Fiona
>
> One possibility is to run this function as a separate step. It doesn't work with comma separated files and it will replace commas in the header (if there is one)
> / per
>
> function comma2point_overwrite( filespec )
> % replaces all occurences of comma (",") with point (".") in a text-file.
> % Note that the file is overwritten, which is the price for high speed.
> file = memmapfile( filespec, 'writable', true );
> comma = uint8(',');
> point = uint8('.');
> file.Data( transpose( file.Data==comma) ) = point;
> delete(file)
> end

Subject: Decimals from comma to dot

From: Akin Sahin

Date: 26 Aug, 2010 12:57:04

Message: 13 of 13

"per isakson" <poi.nospam@bimDOTkthDOT.se> wrote in message <gk2tkh$64l$1@fred.mathworks.com>...
> "Fiona Buckley" <buckley_fiona@yahoo.com> wrote in message <gjvfqq$muv$1@fred.mathworks.com>...
> > Hi,
> > I've got a file in ASCII format with decimal values comma seperated and columns are seperated by tab. The columns either have strings or floating numbers and my first column is in a ISO time-date format.
> > The file to be imported has column headers and titles which I don't need however, I do need all the string data and the floating number data from the general file.
> >
> > I want to write a script to import this data, but as the floating points numbers are comma seperated I can't seem to manage. I previously wrote scripts to do this but up to now my decimals were always dot seperated. No matter what I do now I seem to be stuck. Here under some of the many functions I tried and what happened:
> > A) importdata - gives me a strucutre with one data field and one textdata field but almost all the strings have been trunckated and the numerical values are combined (i.e. 3,34 6,91 9,32 gives me 3 346 91932)
> > B) fopen - error message saying that the variable is undefined
> > C) textscan or textread - error message as the data is not recognised
> > D) strrep - I can't replace the string as I can't open the file
> >
> > Does anyone have an idea???
> > Cheers
> > Fiona
>
> One possibility is to run this function as a separate step. It doesn't work with comma separated files and it will replace commas in the header (if there is one)
> / per
>
> function comma2point_overwrite( filespec )
> % replaces all occurences of comma (",") with point (".") in a text-file.
> % Note that the file is overwritten, which is the price for high speed.
> file = memmapfile( filespec, 'writable', true );
> comma = uint8(',');
> point = uint8('.');
> file.Data( transpose( file.Data==comma) ) = point;
> delete(file)
> end

Thank you Per! This is a great help!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
decimal Sprinceana 21 Sep, 2009 06:58:07
rssFeed for this Thread

Contact us at files@mathworks.com