Using loop to go through several waves and change the points on those waves with a formula.

I have around 400 waves, wave0, wave1, wave2..., wave400. I want to use a formula to change all the points in those waves.

Since this is my first ever programming in Igor Pro, I began with a small step first. I created a sample wave0, and tried to square the points in that wave 2 times using loop.

Function forloop()
   variable i
   for (i=0;i<2;i+=1)
       wave0=wave0^2  
   endfor
end

I get the error shown in the image while compiling.

I feel like I am not getting permission to access the wave0. But the same formula works outside the loop.

Please help me with this.

Thanks

 

Error message

Hi,

If you want to use data (waves, numeric variables, or strings) that lie outside of your function, you need to declare them so Igor knows what specific data you mean to use.

for your simple function, you need to include a declaration of the wave

Function forloop()
   variable i
   wave wave0// this is the declaration
   for (i=0;i<2;i+=1)
       wave0=wave0^2  
   endfor
end

 

That step tells Igor to use wave0.  For future reference you could also have given it a new name in the declaration. such as

Function forloop()
   variable i
   wave newName = wave0// this is the declaration
   for (i=0;i<2;i+=1)
       newName = newName ^2  
   endfor
end

the wave labeled new name only exists within the function and is only a naming device.

Andy

The whole business with WAVE, NVAR and SVAR can be really confusing. It is required because Igor's compiler needs to know what sort of object the name "wave0" refers to in order to compile the right kind of code for it. The compiler runs at a time when wave0 may not exist yet.

To learn more about this, execute this command: DisplayHelpTopic "Accessing Global Variables And Waves"

Just a bit above that help topic is this one: DisplayHelpTopic "Converting a String into a Reference Using $" If you start reading here, you will get to the first one pretty quickly.

Reading the Programming help file is helpful... and if you read it at bed time, it cures insomnia :)

Considering that you want to operate on many different waves, referencing the wave within the function is the thing that allows you to do this, by moving the wave declaration into the loop:

Function forloop()
   variable i
   for (i=0;i<400;i+=1)
       wave ithWave=$"wave"+num2str(i)
       ithWave=ithWave^2  
   endfor
end

 

this might be a bit much at the beginning, but here is another way to do the job that doesn't need a loop:

 

function squareAll1DWaves()
    string wlist = wavelist("wave*",";","")
    Concatenate/FREE/O wList, M_AllWaves
    MultiThread M_allWaves = M_AllWaves[p][q]^2
    Splitwave/O/Name=wlist M_AllWaves
end

The snippet makes a string list of wave names starting with "wave", generates a matrix from the items in this list, squares it, and then splits the matrix back to individual waves.

Edit: oh, the above assumes that all waves are of equal length!

 

And we come back to the usual question whether

MultiThread M_AllWaves = M_AllWaves[p][q]^2

is faster than

MatrixOP M_AllWaves = magsqr(M_AllWaves) magSqr(w)       Returns a real value wave containing the square of a real w  or the squared magnitude of complex w.

 

@sbrijal90l It should be clear to you by now that you have hit one of the hot topics in Igor programming :)

In reply to by s.r.chinn

s.r.chinn wrote:

And we come back to the usual question whether

MultiThread M_AllWaves = M_AllWaves[p][q]^2

is faster than

MatrixOP M_AllWaves = magsqr(M_AllWaves) magSqr(w)       Returns a real value wave containing the square of a real w  or the squared magnitude of complex w.

Don't forget WaveTransform :)

In reply to by tony

Thanks for the exact thing I needed. It compiles very well but it still gives an error when I run the program in command window. Looks like I am missing a major part here.

 

tony wrote:

Considering that you want to operate on many different waves, referencing the wave within the function is the thing that allows you to do this, by moving the wave declaration into the loop:

Function forloop()
   variable i
   for (i=0;i<400;i+=1)
       wave ithWave=$"wave"+num2str(i)
       ithWave=ithWave^2  
   endfor
end

 

igorpro_0.jpg (111.87 KB)

The most common error in a case like this would be that the waves aren't in the current data folder. The best way to look for such errors is using Igor's debugger: DisplayHelpTopic "The Debugger"

And enable debugging on errors: right-click in any procedure window and select Enable Debugger. Right-click again and make sure that Debug on Error and WAVE, NVAR, SVAR Checking is also enabled. Now, you should automatically break into the debugger when such an error occurs, right at the spot in your code that is going wrong.

Oh, and you may want to change #pragma rtglobals=1 to #pragma rtglobals=3, if you are using a sufficiently recent Igor version.

The error you are getting here comes from trying to write 'Function' in the command line. You instead should only write the function's name with () at the end to execute it. The command line does not know what to do with 'Function' here.

In reply to by chozo

This is just awesome. Thank you so much for pointing out that "MAJOR" flaw with my program learning. I really appreciate that and thanks to everyone helping me solve this.

 

chozo wrote:

The error you are getting here comes from trying to write 'Function' in the command line. You instead should only write the function's name with () at the end to execute it. The command line does not know what to do with 'Function' here.