comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: How do I use FORMAT as I would SPRINTF?? Please Help!
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: How do I use FORMAT as I would SPRINTF?? Please Help! [message #32119] Thu, 19 September 2002 05:59 Go to next message
wmconnolley is currently offline  wmconnolley
Messages: 106
Registered: November 2000
Senior Member
Brian Huether <bhuether@earthlink.net> wrote:
> I am quite perplexed by the IDL FORMAT keyword. I am used to MATLAB where I
> can quite simply use the sprintf command.

IDL is like FORTRAN, not C.

-W.

--
William M Connolley | wmc@bas.ac.uk | http://www.nerc-bas.ac.uk/icd/wmc/
Climate Modeller, British Antarctic Survey | Disclaimer: I speak for myself
I'm a .signature virus! copy me into your .signature file & help me spread!
Re: How do I use FORMAT as I would SPRINTF?? Please Help! [message #32120 is a reply to message #32119] Thu, 19 September 2002 05:49 Go to previous messageGo to next message
Randall Skelton is currently offline  Randall Skelton
Messages: 169
Registered: October 2000
Senior Member
Hi Brian,

When you say 'MATLAB' syntax, you are more or less describing C/C++
syntax. Many IDL's semantics have roots in FORTRAN (shutter) and the
format keyword is one of the many ancient artifacts.

A simple example of what you want to do might read:

a = 'test'
b = '10.0'
print, a, b, format="('Size (', A4, ',' , F5.2,')')"

Personally, I like C syntax for these sorts of things and usually do
something like:

a = 'boo'
b = 10S
c = 100.0D

print, format='(%"(Size %s, %i, %d)")', a, b, c

Note the inclusion of the '%' sign just after the opening bracket.

If you search the IDL help for the heading 'Format Codes' you will get all
the gory details. Towards the end, there is a section on C-style print
commands which is what you are interested in.

I will preemptivly answer the following 2 question even though you
haven't asked (yet). Be forewarned that while MATLAB considers all
integers to be type long and non-integers to be type double. IDL does
not. Accordingly, when indexing a loop with more than 32000 elements be
sure to explicitly cast the index to long with something like:

for i=0L, n-1 do begin ... endfor

A few standard casts are:

a = 0B -> byte
b = 0S -> short
c = 0L -> long
d = 0E0 -> float
e = 0D0 -> double

This may seem like a pain in the ass at first but for me it means I can do
much larger calculations in IDL as I often only need float precision so
MATLAB's default to double is wasteful.

Another pitfall is the indexing of arrays in IDL compared with MATLAB. If
you are using arrays and pretending they are matrices, be forewarned that
IDL uses C-style array handling which is (columns,rows) NOT (rows,columns)
which is the standard MATLAB matrix and customary linear algebra notation.
You will want to again read the online help under the heading, "Arrays
and Matrices".

Good luck,
Randall


On Thu, 19 Sep 2002, Brian Huether wrote:

> Date: Thu, 19 Sep 2002 12:23:57 +0200
> From: Brian Huether <bhuether@earthlink.net>
> Newsgroups: comp.lang.idl-pvwave
> Subject: How do I use FORMAT as I would SPRINTF?? Please Help!
>
> I am quite perplexed by the IDL FORMAT keyword. I am used to MATLAB where I
> can quite simply use the sprintf command. For instance, in MATLAB, the
> command
>
> s = sprintf('IDL has been driving me crazy for the last %d days', 10)
>
> would result in a string that contains
>
> 'IDL has been driving me crazy for the last 10 days'
>
> What I really need to do is something like this:
>
> s = sprintf('SIZE(%s, /%s)', arrayname, somekeyword)
>
> So if arrayname is A and somekeyword is TYPE, then the resulting string
> would be
>
> 'SIZE(A, /TYPE)'
>
> It is a meaningless example but hopefully you see what I want to do (i.e. I
> need to execute IDL commands in a loop, where each time through, the command
> input parameters are varying and are specified by indexing arrays that
> contain the parameters)
>
>
>
Re: How do I use FORMAT as I would SPRINTF?? Please Help! [message #32121 is a reply to message #32120] Thu, 19 September 2002 05:09 Go to previous messageGo to next message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
Brian Huether wrote:
> I am quite perplexed by the IDL FORMAT keyword. I am used to MATLAB where I
> can quite simply use the sprintf command. For instance, in MATLAB, the
> command
>
> s = sprintf('IDL has been driving me crazy for the last %d days', 10)
>
> would result in a string that contains
>
> 'IDL has been driving me crazy for the last 10 days'
>
> What I really need to do is something like this:
>
> s = sprintf('SIZE(%s, /%s)', arrayname, somekeyword)
>
> So if arrayname is A and somekeyword is TYPE, then the resulting string
> would be
>
> 'SIZE(A, /TYPE)'
>
> It is a meaningless example but hopefully you see what I want to do (i.e. I
> need to execute IDL commands in a loop, where each time through, the command
> input parameters are varying and are specified by indexing arrays that
> contain the parameters)
>
>


nice idea, did someone else like to have such a function,
I believe it's quite easy to solve.

For example:

FUNCTION sprintf,STRING,number

per=STRPOS(STRING,'%')
sign=STRMID(STRING,per,2)
CASE sign OF
'%d' : str_num=STRTRIM(STRING(number,format='(I2)'),2)
ELSE:
ENDCASE

RETURN,STRMID(STRING,0,per)+str_num+STRMID(STRING,per+2,3276 7)
END


This could be a bit more enhanced. But I am not sure what this function
did in mathlab or if you only like to add a number as string in your output.

There are some routines from our library which may be useful


http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/find_form.tar.gz
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/number_format.tar.gz
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/n2s.tar.gz

or as idl5.5 binary

http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/find_form.sav
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/number_format.sav
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/n2s.sav

For example:


print,'I have learned wonderful things in IDL in the last '+n2s(10)+ $
' days!!'

I have learned wonderful things in IDL in the last 10 days!!




best regards

Reimar





--
Reimar Bauer

Institut fuer Stratosphaerische Chemie (ICG-I)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
------------------------------------------------------------ -------
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
============================================================ =======
Re: How do I use FORMAT as I would SPRINTF?? Please Help! [message #32218 is a reply to message #32119] Thu, 19 September 2002 07:41 Go to previous message
Richard Younger is currently offline  Richard Younger
Messages: 43
Registered: November 2000
Member
wmc@bas.ac.uk wrote:
>
> IDL is like FORTRAN, not C.
>

Well, yes. But if you're running, er, 5.4 I think, or later, the FORMAT
keyword supports printf style tokens. Just prepend a % to the format string.

E.g. (nicked from the IDL help files):

PRINT, FORMAT='(%"I have %d monkeys, %s.")', 23, 'Scott'

Look at the bottom of the IDL help topic "Format Codes." The common codes are
all supported.

Now, admittedly, most of us are more familiar with the traditional
FORTRAN-style, so if you post here you may cause some confusion. Do whatever
is the most productive, I say.

Good Luck,
Rich

--
Richard Younger
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Indexing into Structures???
Next Topic: How do I use FORMAT as I would SPRINTF?? Please Help!

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 17:37:59 PDT 2025

Total time taken to generate the page: 0.00533 seconds