How do I use FORMAT as I would SPRINTF?? Please Help! [message #32125] |
Thu, 19 September 2002 03:23  |
Brian Huether
Messages: 12 Registered: September 2002
|
Junior Member |
|
|
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 #32216 is a reply to message #32125] |
Thu, 19 September 2002 08:54  |
Brian Huether
Messages: 12 Registered: September 2002
|
Junior Member |
|
|
Thanks for all the replies. Looks like I will have some MATLAB licenses
available to me next week so I'll be able to go back to my familiar ways!
The only thing I was able to do with IDL during my short time with it was
write a routine that creates an HDF file based on inputted data and
attributes. It is a fairly general routine and seems to work.
-brian
"Brian Huether" <bhuether@earthlink.net> schrieb im Newsbeitrag
news:3d89a431$1_8@news.teranews.com...
> 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 #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.
|
|
|