really basic iteration

Hi everyone,

here is the thing. i want to write a simple macro that, let's say, differentiate waves that have the same root name. i e. wave0, wave1, wave 2, etc. so, instead of going
differentiate wave0
differentitate wave1
differentiate wave2
...etc

i want to do something like

variable n = 0
differentiate wave(n)
n+=1
etc

but i have have a problem with the basic syntax, and i am not able to find the right way to do it in the monstuous manual.

so, i woul really appreciate some feedback on this simple matter.
thanks in advance.

have a nice weekend

P
// this function iterates through all waves of a given template basename
Function DoMyThingOnSetofWaves(basename)
     string basename

     // define variables and strings
     variable ic, nt
     string theList, theOne
     // get a (string)  list of all wave names that fit the template
     theList = WaveList(basename,";","")
     nt = ItemsInList(theList)
     // iterate through the list
     for(ic=0;ic<nt;ic+=1)
          // get the next name that fits the rule
          theOne = StringFromList(ic,theList)
          // convert the string name to a wave reference
          wave wwave = $theOne
          // do it to it
          DoMyThingOnOneWave(wwave)
     endfor
     return 0
end

// this function does something on only one wave
Function DoMyThingOnOneWave(ww)
     wave ww

     differentiate ww
     return 0
end


Once you do this, on the command line, you can try ...

make/n=100 sinx0=sin(x), sinx1=sin(1/x), sinx2=sin(x^2), sinx3=sin(x^3), sinx4=2*sin(x),cosx0=cos(x)
display sinx0,sinx1,sinx2,sinx3,sinx4,cosx0
domythingonsetofwaves("sinx*")


HTH

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
jjweimer wrote:
nt = numpnts(theList)


This line should be:

nt = itemsinlist(theList)

(I actually don't know, why the compiler throws no error.)

awirsing wrote:
jjweimer wrote:
nt = numpnts(theList)


This line should be:

nt = itemsinlist(theList)


Fixed. Thank you.

awirsing wrote:
(I actually don't know, why the compiler throws no error.)


I wrote it here, not in Igor.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
jjweimer wrote:
awirsing wrote:
(I actually don't know, why the compiler throws no error.)


I wrote it here, not in Igor.


But I pasted it into the procedure window and it compiled without error, although numpnts is supposed to work on waves (and not on strings).
Hi guys,

I tried the function and it works just fine. Many thanks.
I have a small problem though. I need to preserve the original waves, meaning each derivative should be stored in a different wave. Now, if I try to duplicate the source waves before differentiation or store the derivative in a new wave I encounter again the same problem, meaning having to rename output waves in numerical order, such as wave0_DIF, wave1_DIF, wave3_DIF. Could you please give a suggestion to solve this little problem?
Thanks

P
pjfd wrote:
I need to preserve the original waves, meaning each derivative should be stored in a different wave. Now, if I try to duplicate the source waves before differentiation or store the derivative in a new wave I encounter again the same problem, meaning having to rename output waves in numerical order, such as wave0_DIF, wave1_DIF, wave3_DIF. Could you please give a suggestion to solve this little problem?


// this function iterates through all waves of a given template basename
Function DoMyThingOnSetofWaves(basename)
     string basename
 
     // define variables and strings
     variable ic, nt
     string theList, theOne,theSecond
     // get a (string)  list of all wave names that fit the template
     theList = WaveList(basename,";","")
     nt = ItemsInList(theList)
     // iterate through the list
     for(ic=0;ic<nt;ic+=1)
          // get the next name that fits the rule
          theOne = StringFromList(ic,theList)
          // create output wave name
          theSecond = "DIF_" + theOne
          // duplicate source wave
          duplicate/o $theOne $theSecond
          // convert the string name to a wave reference
          wave wwave = $theSecond
          // do it to it
          DoMyThingOnOneWave(wwave)
     endfor
     return 0
end


I prepended the 'DIF_' suffix, because otherwise you will end up with something like wave0_DIF_DIF if you call the function twice.

A.
thanks. that has all been useful and works fine.
final point: i would like to plot in one graph multiple waves, such as
wave0 vs wave1, wave 2 vs wave3, wave4 vs wave 5, ... and so on. is there a simple way to do it with a function? i have more than a thousand pairs, so i cannot go doing it by hand

P
pjfd wrote:
thanks. that has all been useful and works fine.
final point: i would like to plot in one graph multiple waves, such as
wave0 vs wave1, wave 2 vs wave3, wave4 vs wave 5, ... and so on. is there a simple way to do it with a function? i have more than a thousand pairs, so i cannot go doing it by hand

P


It can be done, but it strongly depends on how your wave are named.
Steps that may be involved are:
create a stringlist for the y waves and the x waves (see wavelist, sortlist, etc.)
iterate trough the lists and convert each item into an wave reference (see stringfromlist, wave, for-endfor, etc.)
and append them to the graph (see appendtograph)

Andreas
awirsing wrote:
pjfd wrote:

wave0 vs wave1, wave 2 vs wave3, wave4 vs wave 5, ... and so on.

It can be done, but it strongly depends on how your wave are named.


With the short list above using only ten waves where they are plotted as odd versus even, you would be able to generate the x-wave and y-wave lists using a GrepList command of the type ...

string theList = WaveList("",";","")
string thexList = GrepList(theList,"wave[13579]")
string theyList = GrepList(theList,"wave[02468]")


Extending this code to work for sequences of waves beyond ten is left as an exercise for the reader :-)

ANSWER:

string thexList = GrepList(theList,"wave.*[13579]")


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
pjfd wrote:
i would like to plot in one graph multiple waves, such as
wave0 vs wave1, wave 2 vs wave3, wave4 vs wave 5, ... and so on.


Presuming your wave names are as quoted here, the recent development release of LinkDisplay will now allow you to do the following at the command line or in a procedure:

string/G theYGrep = "wave.*[02468]"
string/G theXGrep = "wave.*[13579]"
string/G theYWaveList = GrepList(WaveList("*",";",""),theYGrep)
LinkDisplay#LinkWithGREP(theYGrep,theXGrep)
display
LinkDisplay#LAppendtoGraph(theYWaveList)


The last two lines presume that you want to show all y-waves on one graph. If each y-wave is to be shown on its own graph, do instead ...

LinkDisplay#LDisplay(theYWaveList)


The help file for LinkDisplay explains briefly the command syntax.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
pjfd, since you find yourself doing these sort of tasks often, you might want to create wave references inside a loop, like:

display
variable i=0
for (i=0; i<1000; i+=1) // string reference
    wave thisWave = $("baseName" + i)
    appendToGraph thisWave
endfor
for (i=0; i<1000; i+=1) // string command
    sprintf cmd, "print waveMax(%s)", ("baseName" + num2str(i))
    Execute cmd
endfor


I'm not sure whether this is "recommended" but I find it useful when I don't want to use wave0 wave1 wave2 style names.