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