Re: modifying str2double

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: modifying str2double

Rik-4
On 04/10/2020 06:38 AM, [hidden email] wrote:
Subject:
Re: About str2double() and exponent identifier
From:
Ian McCallion [hidden email]
Date:
04/09/2020 11:03 PM
To:
José Luis García Pallero [hidden email]
CC:
Octave Maintainers [hidden email]
List-Post:
[hidden email]
Precedence:
list
MIME-Version:
1.0
References:
[hidden email]
In-Reply-To:
[hidden email]
Message-ID:
[hidden email]
Content-Type:
multipart/alternative; boundary="000000000000ccb0c305a2e97ec9"
Message:
3

On Fri, 10 Apr 2020 at 01:47, José Luis García Pallero <[hidden email]> wrote:
Hello,

I've noted that the function str2double() only works with exponent
identifier as 'e' or 'E', but not with 'd' or 'D', that are common
identifiers in files generated using Fortran. Then

str2double('1e2') is 100, but
str2double('1d2') is NaN

This behavior is the same in Octave as well as Matlab. Exists any
technical reason in order to not consider the identifiers 'd' or 'D'
for the conversion of the reason of the Octave's behavior is for
matching the Matlab's one?

I find str2double() more convenient in a task than str2num() as the
first one is faster, but my dataset uses 'D' as exponent identifier

str2double is a compiled function, but str2num uses eval which will be a lot slower as you say.

You would almost certainly find it faster to first replace 'd' with 'e'. e.g.:

function d= mystr2double(d)
   d(d == 'd')='e';
   d = str2double(d);
endfunction

Cheers... Ian

This was my first thought as well.  We would want to introduce a new feature in to Octave, and an incompatibility with Matlab, only generally when it is clearly useful, would benefit many users, and can't be accomplished in any other manner.  This feels like a feature that would benefit a small number of users and for which there are abundant workarounds.  Ian proposed a solution within Octave.  Outside of Octave you could use the search and replace feature of a standard text editor.  Or if you are on a Linux system then

sed -e 's/d/e/g' FILENAME > NEW_FILENAME

--Rik