| 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   |
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
============================================================ =======
|
|
|
|