Early on the learning curve: for loops
dcnicholls
Tue, 11/30/2010 - 01:18 am
I'm trying to familiarize myself with the way Igor works, but I'm lacking certain key information on how to do things. In particular, how to use do loops inline(possible?) or in a Procedure. The following trivial example:
Make/N=4/D xx,yy
xx=1
yy=0
for (ii=1; ii<4; ii+=1)
xx=xx+1
yy=xx*xx
endfor
print xx,yy
gives me an error ('expected wave name, variable name or operation' at the "for" line) when I type in the commands sequentially like Python will let you do. So I presume sequential commands like this aren't allowed.
If I enter the same sequence into a new Procedure window, it compiles OK (ie no error message), and I can save it. But now I need to understand how to invoke/run it.
Igor is sufficiently different than anything I've used before that I can't work out the analogies with what I know. The "Guided Tour" section uses a much more detailed example, which I can emulate but which I can't see how to apply in this simple case.
Help appreciated.
make/n=4/d xx,yy
xx = p
yy = xx^2
//or yy = p^2
End
November 30, 2010 at 02:06 am - Permalink
I can get do-while working as a Macro:
Macro test()
Make/N=4/D/O xx,yy
xx=1
yy=0
Variable ii=1
do
xx=xx+1
yy=xx*xx
ii+=1
while(ii<4)
print xx,yy
EndMacro
which I can immediately run from the macros menu item. However, for-loops won't run in macros, I find. So I was wondering how to create a procedure rather than a macro (the manuals say procedures are preferred over macros), and then how to run it in the absence of a menu option.
DN
November 30, 2010 at 04:28 am - Permalink
Make/O/N=4/D xx,yy
xx=1
yy=0
variable ii //this is missing in your first example
for (ii=1; ii<4; ii+=1)
xx=xx+1 //not sure what this should do in a loop
yy=xx*xx
endfor
print xx,yy
end
you can execute this code by typing
into the command line. Check out the chapter IV-3; Programming; User-defined functions of the manual.
November 30, 2010 at 04:57 am - Permalink
November 30, 2010 at 05:03 am - Permalink
November 30, 2010 at 11:14 am - Permalink
November 30, 2010 at 06:34 pm - Permalink