For Programmers: Free Programming Magazines  


Home > Archive > Fortran > March 2007 > fortran equivalent of matlab num2str









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author fortran equivalent of matlab num2str
louisJ

2007-03-07, 7:08 pm

Hello

does someone know how to convert an integer or a number to a string of
character in fortran 90?

Or What isthe corresponding fortran statement for Matlab num2str?

Thank you.

Bart Vandewoestyne

2007-03-07, 7:08 pm

On 2007-03-07, louisJ <plumerault@gmail.com> wrote:
>
> does someone know how to convert an integer or a number to a string of
> character in fortran 90?


You can do a search on google for 'internal write'. I think that
will answer your question.

Best wishes,
Bart

--
"Share what you know. Learn what you don't."
Brooks Moses

2007-03-07, 7:08 pm

louisJ wrote:
> does someone know how to convert an integer or a number to a string of
> character in fortran 90?
>
> Or What isthe corresponding fortran statement for Matlab num2str?


Coincidentally, I was just a minute ago looking at the bit of the GNU
Fortran documentation which covers this (well, almost). I even still
have the window open to cut-and-paste from. Thus:

---------------------------------------------------------------------
No intrinsic exists to convert a printable character string to a
numerical value. For example, there is no intrinsic that, given the
CHARACTER value '154', returns an INTEGER or REAL value with the value
154. Instead, you can use internal-file I/O to do this kind of
conversion. For example:

program read_val
integer value
character(len=10) string
string = ’154’
read (string,’(I10)’) value
print *, value
end program read_val

---------------------------------------------------------------------

What you want is the inverse problem, which is essentially the same
except using WRITE instead of READ.

- Brooks


--
The "bmoses-nospam" address is valid; no unmunging needed.
Beliavsky

2007-03-07, 7:08 pm

On Mar 7, 3:58 pm, Bart Vandewoestyne
<MyFirstName.MyLastN...@telenet.be> wrote:
> On 2007-03-07, louisJ <plumera...@gmail.com> wrote:
>
>
>
>
> You can do a search on google for 'internal write'. I think that
> will answer your question.


Yes. An embryonic Wikibook FAQ at http://en.wikibooks.org/wiki/Fortran_FAQ
answers this under the question "Q: How can I convert a string to an
integer and other types? What about the reverse?"

I hope c.l.f. readers will add to FAQ above, since the "official" one
edited by Keith Bierman is about 10 years old.

andy2O

2007-03-07, 7:08 pm

On 7 Mar, 20:46, "louisJ" <plumera...@gmail.com> wrote:
> Hello
>
> does someone know how to convert an integer or a number to a string of
> character in fortran 90?
>
> Or What isthe corresponding fortran statement for Matlab num2str?
>
> Thank you.


Hi louiseJ,

Bart is quite correct - you need to do something called an 'internal
write'. This is true in FORTRAN77 and Fortran95. The test code below
which I wrote to describe this to a friend illustrates this for an
integer and a real variable. Two of the internal writes use the
default '*' format. The other one shows that you can specify any
suitable format (here I've used '(i10)') to control the format of the
internal write.

Best wishes,
andy

program test

implicit none

character(20) :: string

integer n
real a

n=10
write(string,*) n
print*, 'Integer is n = ',n
print*, 'String is (with * placed before'
print*, 'and after * to show extent of whitespace) is:'
print*, '*'//string//'*'
print*

write(string,fmt=100) n
100 format (i10)
print*, 'Integer is n = ',n
print*, 'String with format (i10) is (with * placed'
print*, 'before and after * to show extent of whitespace) is:'
print*, '*'//string//'*'
print*

a=123.456
write(string,*) a
print*, 'Real is a = ',a
print*, 'String is (with * placed before '
print*, 'and after * to show extent of whitespace) is:'
print*, '*'//string//'*'

end program test



andy2O

2007-03-07, 7:08 pm

On 7 Mar, 20:46, "louisJ" <plumera...@gmail.com> wrote:
> Hello
>
> does someone know how to convert an integer or a number to a string of
> character in fortran 90?
>
> Or What isthe corresponding fortran statement for Matlab num2str?
>
> Thank you.


Hi louiseJ,

Bart is quite correct - you need to do something called an 'internal
write'. This is true in FORTRAN77 and Fortran95. The test code below
which I wrote to describe this to a friend illustrates this for an
integer and a real variable. Two of the internal writes use the
default '*' format. The other one shows that you can specify any
suitable format (here I've used '(i10)') to control the format of the
internal write.

Best wishes,
andy

program test

implicit none

character(20) :: string

integer n
real a

n=10
write(string,*) n
print*, 'Integer is n = ',n
print*, 'String is (with * placed before'
print*, 'and after * to show extent of whitespace) is:'
print*, '*'//string//'*'
print*

write(string,fmt=100) n
100 format (i10)
print*, 'Integer is n = ',n
print*, 'String with format (i10) is (with * placed'
print*, 'before and after * to show extent of whitespace) is:'
print*, '*'//string//'*'
print*

a=123.456
write(string,*) a
print*, 'Real is a = ',a
print*, 'String is (with * placed before '
print*, 'and after * to show extent of whitespace) is:'
print*, '*'//string//'*'

end program test



Lane Straatman

2007-03-07, 7:08 pm


"Beliavsky" <beliavsky@aol.com> wrote in message
news:1173303891.689741.175340@v33g2000cwv.googlegroups.com...

> Yes. An embryonic Wikibook FAQ at http://en.wikibooks.org/wiki/Fortran_FAQ
> answers this under the question "Q: How can I convert a string to an
> integer and other types? What about the reverse?"
>
> I hope c.l.f. readers will add to FAQ above, since the "official" one
> edited by Keith Bierman is about 10 years old.

As a Frequently Asking Questioner, is adding to the wiki article something I
should do? If I understand the wiki idea, it's run on volunteerism. I
asked about a FAQ list a couple months back, when I was unable to do much
for lack of bandwidth. Now, I download music. I think there's enough
quality Q&A around here that one could cut and paste some exchanges into a
more permanent home.
--
LS


Beliavsky

2007-03-07, 7:08 pm

On Mar 7, 6:03 pm, "Lane Straatman" <inva...@invalid.net> wrote:

>
> As a Frequently Asking Questioner, is adding to the wiki article something I
> should do? If I understand the wiki idea, it's run on volunteerism. I
> asked about a FAQ list a couple months back, when I was unable to do much
> for lack of bandwidth.


You can edit the FAQ to add questions that commonly arise, whether or
not you know the answers. If I cannot answer a question with
confidence and no one else answers the question on the wikibook, I may
post the question here and incorporate the answers into the Wikibook.

Lane Straatman

2007-03-16, 7:07 pm


"Beliavsky" <beliavsky@aol.com> wrote in message
news:1173303891.689741.175340@v33g2000cwv.googlegroups.com...


> Yes. An embryonic Wikibook FAQ at http://en.wikibooks.org/wiki/Fortran_FAQ
> answers this under the question "Q: How can I convert a string to an
> integer and other types? What about the reverse?"
>
> I hope c.l.f. readers will add to FAQ above, since the "official" one
> edited by Keith Bierman is about 10 years old.

Oh gosh! It's easier to go in and change than you think. How do I get that
nice rectangle around a code snippet?
--
LS


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com