comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Function with something like [2:*] as input
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Function with something like [2:*] as input [message #87137] Thu, 09 January 2014 12:06 Go to next message
andry is currently offline  andry
Messages: 7
Registered: March 2013
Junior Member
Hi,

Could anybody help on how I can write a function so that the input will be something like the indices of an array:

For example, my function would accept a call of this format:

a= my_function([1:3]) ; to say that it will process element 1 to 3 of an array
a= my_function([[1:*]) ; to say that it will process everything from element 1 to the end. This is because most of the time, I would not know how many data I have in some array.

My problem is that I can not see how I can read that input from within the function.

I am not sure this makes sense but it will facilitate the analysis of my data.

Thanks in advance for your help,

Andry
Re: Function with something like [2:*] as input [message #87138 is a reply to message #87137] Thu, 09 January 2014 12:57 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
Here is one way to do it. Create the function below, then

IDL> array = findgen(20)
IDL> print, SubArray(array, 1, 3)
IDL> print, SubArray(array, 1, N_Elements(array)-1)
IDL> print, SubArray(array, 1, -1)


function SubArray, array, index1, index2
return, array[index1:index2]
end
Re: Function with something like [2:*] as input [message #87143 is a reply to message #87138] Fri, 10 January 2014 04:34 Go to previous messageGo to next message
andry is currently offline  andry
Messages: 7
Registered: March 2013
Junior Member
On Thursday, January 9, 2014 5:27:31 PM UTC-3:30, Matthew Argall wrote:
> Here is one way to do it. Create the function below, then
>
>
>
> IDL> array = findgen(20)
>
> IDL> print, SubArray(array, 1, 3)
>
> IDL> print, SubArray(array, 1, N_Elements(array)-1)
>
> IDL> print, SubArray(array, 1, -1)
>
>
>
>
>
> function SubArray, array, index1, index2
>
> return, array[index1:index2]
>
> end

Thanks,

I did not know about the "-1" index.

Andry
Re: Function with something like [2:*] as input [message #87145 is a reply to message #87137] Fri, 10 January 2014 07:28 Go to previous messageGo to next message
chris_torrence@NOSPAM is currently offline  chris_torrence@NOSPAM
Messages: 528
Registered: March 2007
Senior Member
Hi Andry,

If you really wanted to get fancy, and you have IDL 8.0 or later, you could use operator overloading. You could create an object class that is a subclass of IDL_Object, and then implement the _overloadBracketsRightSide method. Then you could do things like:

myfunc = MyFunctionClass()
a = myfunc[1:3] ; looks just like array indexing!

See the examples at:
http://www.exelisvis.com/docs/Overloading_the_Array_In.html

Cheers,
Chris
ExelisVIS
Re: Function with something like [2:*] as input [message #87146 is a reply to message #87145] Fri, 10 January 2014 07:39 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Chris Torrence writes:

> If you really wanted to get fancy, and you have IDL 8.0 or later, you could use operator overloading. You could create an object class that is a subclass of IDL_Object, and then implement the _overloadBracketsRightSide method. Then you could do things like:
>
> myfunc = MyFunctionClass()
> a = myfunc[1:3] ; looks just like array indexing!
>
> See the examples at:
> http://www.exelisvis.com/docs/Overloading_the_Array_In.html

Right. I was going to mention that just after I explained the value of
using positional and keyword parameters. ;-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Download files from the web
Next Topic: NITER in MPFITFUN

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 11:37:27 PDT 2025

Total time taken to generate the page: 0.00486 seconds