io format specifier question

View: New views
7 Messages — Rating Filter:   Alert me  

io format specifier question

by marco restelli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear list members,
   I need to read an ASCII file containing floating point numbers as

 0.112977-141

which stands for 0.112977E-141 (i.e., the E is omitted). The point is,
fscanf(fp,'%e') treats this as two numbers, and using

line = fgets(fp);
x = str2num(line);

I get x = 0.112977 - 141 = -140.89

Is there a way to make octave understand this format?

(Notice that writing 0.112977-141 in place of 0.112977E-141 is
standard conforming behavior for FORTRAN programs)

Thank you,
   Marco
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www.cae.wisc.edu/mailman/listinfo/help-octave

Re: io format specifier question

by A. Kalten :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 1 Jul 2008 20:02:36 +0200
"marco restelli" <mrestelli@...> wrote:

>    I need to read an ASCII file containing floating point numbers as
>
>  0.112977-141
>
> which stands for 0.112977E-141 (i.e., the E is omitted). The point is,
> fscanf(fp,'%e') treats this as two numbers
>

If you're on a unix system, just do:

sed s/-/e-/ file > file-new

This will convert all "0.112977-141" to "0.112977e-141"

AK

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

Re: io format specifier question

by A. Kalten :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 1 Jul 2008 14:24:59 -0400
"A. Kalten" <akalten@...> wrote:

>
> If you're on a unix system, just do:
>
> sed s/-/e-/ file > file-new
>
> This will convert all "0.112977-141" to "0.112977e-141"
>

I should amend that command in case some values are negative,
e.g. -0.112977-141

sed 's/\([0-9]\)\(-\)/\1e\2/' file > file-new

This will replace only the minus sign after the number.

AK

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

Re: io format specifier question

by Carlo de Falco-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 01/lug/08, at 19:24, A. Kalten wrote:

> On Tue, 1 Jul 2008 20:02:36 +0200
> "marco restelli" <mrestelli@...> wrote:
>
>>   I need to read an ASCII file containing floating point numbers as
>>
>> 0.112977-141
>>
>> which stands for 0.112977E-141 (i.e., the E is omitted). The point  
>> is,
>> fscanf(fp,'%e') treats this as two numbers
>>
>
> If you're on a unix system, just do:
>
> sed s/-/e-/ file > file-new
>
> This will convert all "0.112977-141" to "0.112977e-141"
>
> AK

I am afraid that would mess up with negative numbers...
Try:

sed 's/\([0-9]*.[0-9]*\)\([-+]\)/\1e\2/' filename > newfilename

this asumes you have one number per row...
anyway, if it is easy to read this format in fortran, you can easily
wrap a small fortran program in a DLD function to call it from octave.

c.

P.S. Ciao Marco :)


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

Re: io format specifier question

by marco restelli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jul 1, 2008 at 9:17 PM, Carlo de Falco <carlo.defalco@...> wrote:

>
> On 01/lug/08, at 19:24, A. Kalten wrote:
>
>> On Tue, 1 Jul 2008 20:02:36 +0200
>> "marco restelli" <mrestelli@...> wrote:
>>
>>>  I need to read an ASCII file containing floating point numbers as
>>>
>>> 0.112977-141
>>>
>>> which stands for 0.112977E-141 (i.e., the E is omitted). The point is,
>>> fscanf(fp,'%e') treats this as two numbers
>>>
>>
>> If you're on a unix system, just do:
>>
>> sed s/-/e-/ file > file-new
>>
>> This will convert all "0.112977-141" to "0.112977e-141"
>>
>> AK
>
> I am afraid that would mess up with negative numbers...
> Try:
>
> sed 's/\([0-9]*.[0-9]*\)\([-+]\)/\1e\2/' filename > newfilename
>
> this asumes you have one number per row...
> anyway, if it is easy to read this format in fortran, you can easily
> wrap a small fortran program in a DLD function to call it from octave.
>

Thank you AK, Carlo
   yes, your solution is an option.

Still I wonder whether there is a format descriptor in octave which
can handle this without preprocessing. The reason why I am insisting
is that all the home made preprocessing 1) tends to make the data
processing cumbersome when many files/people/systems are involved and
2) is likely to produce wrong results in various corner cases, which
might be standard in some sense (in my case, according to the FORTRAN
standard), but which I fail to take into account.

Anyway, thank you for the suggestion,

Marco




> c.
>
> P.S. Ciao Marco :)
>
>
>
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www.cae.wisc.edu/mailman/listinfo/help-octave

Re: io format specifier question

by A. Kalten :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 1 Jul 2008 21:37:38 +0200
"marco restelli" <mrestelli@...> wrote:

>
> Still I wonder whether there is a format descriptor in octave which
> can handle this without preprocessing. The reason why I am insisting
> is that all the home made preprocessing 1) tends to make the data
> processing cumbersome when many files/people/systems are involved and
> 2) is likely to produce wrong results in various corner cases, which
> might be standard in some sense (in my case, according to the FORTRAN
> standard), but which I fail to take into account.
>

The other option is to do some custom programming in octave.  After
reading the string value from the file, then use the string functions
such as strmatch, split, etc. to transform "0.44781-45" to "0.44781e-45"
and then load the result into a variable.  It would take a little effort
but it should give you the solution that you need.

AK



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

Re: io format specifier question

by A. Kalten :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 1 Jul 2008 21:37:38 +0200
"marco restelli" <mrestelli@...> wrote:

>
> Still I wonder whether there is a format descriptor in octave which
> can handle this without preprocessing. The reason why I am insisting
> is that all the home made preprocessing 1) tends to make the data
> processing cumbersome when many files/people/systems are involved and
> 2) is likely to produce wrong results in various corner cases, which
> might be standard in some sense (in my case, according to the FORTRAN
> standard), but which I fail to take into account.
>

Sorry about all the posts, but I'm still learning about octave myself.

There could some function somewhere to read Fortran data, but I
wouldn't know.

However, the octave regexprep() string function should work.  Just
read the data into string s and convert with:

regexprep(s,'([0-9])([+-])','$1e$2')

Then convert back to a double value with str2double()

The regular expressions can be modified to accommodate any
data variation.

This seems the simplest solution.

AK

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www.cae.wisc.edu/mailman/listinfo/help-octave
LightInTheBox - Buy quality products at wholesale price