Re: How do I use FORMAT as I would SPRINTF?? Please Help! [message #32217 is a reply to message #32125] |
Thu, 19 September 2002 08:06  |
James Kuyper
Messages: 425 Registered: March 2000
|
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)
IDL> arrayname = 'A'
IDL> somekeyword = 'TYPE'
IDL> s = 'SIZE(' + arrayname + ', /' + somekeyword + ')'
IDL> print,s
SIZE(A, /TYPE)
This technique is simple and straightforward, but doesn't work for
numbers; you need to convert them to strings. That's what the STRING()
function is for, and it takes a FORMAT argument. That format argument
uses the same codes as the 'print' command, which include C-like formats
such as %f.
s = STRING(arrayname, somekeyword, FORMAT='(%"SIZE(%s, /%s)")')
I strongly recommend reviewing the online help; look in the index for
'printf-style format codes'. The rules aren't quite the same as for C
(and presumably for MATLAB), often in vary frustrating ways.
|
|
|