Dear Marcos,
There is two way to accomplish this by using the following IDL functions:
1- EXECUTE()
2- SCOPE_VARFETCH()
Using EXECUTE you can do exactly what you are asking for. The EXECUTE function compiles and executes one or more IDL statements contained in a string at run-time. But it is limited by two factors:
-- The need to compile the string at runtime makes EXECUTE inefficient in terms of speed.
-- The EXECUTE function cannot be used in code that runs in the IDL Virtual Machine.
You can read more about it in the IDL Online Help. There you can find this following example:
Create a string that holds a valid IDL command and execute the command by entering:
com = 'PLOT, [0,1]'
void = EXECUTE(com)
Execute the contents of the string by entering:
R = EXECUTE(com)
A plot should appear. You can confirm that the string was successfully compiled and executed by checking that the value of R is 1.
There is another interesting example that I can give you. For example, if you have a variable that has assigned a numerical value. Then that variable name becomes a string. You can extract the information about the numerical value of that string (that previously was a variable) by doing the following:
IDL> top=3.3
IDL> aa='top'
IDL> bb=execute('print,' + aa)
3.30000
The other case is a more robust way of programming: SCOPE_VARFETCH() . You will be able to use the following also working in the Virtual Machine (while EXECUTE() doesn't allow that). SCOPE_VARFETCH() allows you to create a variable from a string. Please, take a look at the IDL Online Help also.
An interesting example is:
The following creates a variable in the main program scope, named varname. It sets the value using the DIST function, and then uses SURFACE to display it:
(SCOPE_VARFETCH('varname', /ENTER)) = DIST(128)
SURFACE, SCOPE_VARFETCH('varname')
Note the use of parenthesis on the left-hand side of the first equation, this is necessary fo SCOPE_VARFETH() to work.
I hope this is helpful.
Regards,
Fernando
|