Problem with for loop in user defined function

Hi All,

I have written a code whose core is reported below. The problem is that when I run the last two indented for loop, in the resulting "summation" waves (running over the j index) I find only zero values (the syntax seems fine, I don't have a warning message when I run the function). I had already this problem when I was trying to have everything in the same "big" for loop (the first reported below). To try to fix the problem, I splitted up the code in two different for loop, as reported below, but still I have problems. I have no idea how to solve it, if somebody could help me it would be great....and really appreciated!

Many thanks in advance!

Duncan
(a desperate postdoc :-) )

for (j = 0; j<lambda_points; j+=1)
        Wave w = $("exp_dump_" + num2str(j))
            for (i = 0; i<points; i+=1)
                N1s = num2str(j) + "N1s" + num2str(i)
                Make/O/N=161 $N1s/WAVE=sim
                SetScale/I x 397, 405, "161", $N1s
                Variable x1 = BEs[i]
                Variable x2 = w[i]
                sim =  x2*(0.0355 + 0.95*((V_index)*(exp(-((BE_scale - x1)/(0.525*(FWHM)))^2))
            endfor
       
endfor
   
for (j = 0; j<lambda_points; j+=1)     
          for (i = 0; i<points; i+=1)
        N1s = num2str(j) + "N1s" + num2str(i)
        summation_ = "summation_" + num2str(j)
        Make/O/N=161 $summation_/WAVE=wout
        SetScale/I x 397, 405, "161", $summation_
            Wave p = $(num2str(j) + "N1s" + num2str(i))
            Variable x3 = p
        wout = wout + x3
          endfor
endfor
It's not clear how you want this sum to work. Are you summing all waves into one value or are your summing each wave into a separate row in wout ... or something else?
jtigor wrote:
It's not clear how you want this sum to work. Are you summing all waves into one value or are your summing each wave into a separate row in wout ... or something else?


So, I would like to sum the all i-waves generated during the first loop, so to sum together the "N1s" waves with different i index but different j index. Doing this, after the second loop I would like to have a number of "summation" waves equal to "lambda_points", each one obtained by the summation of all the "N1s" waves bearing the same j index (but of course different i).

I hope in this way it will be more clear....many many thanks for the help,

Duncan
Hi All,

I found the bug: I was treating my "N1s" waves as a variable (Variable x3 = p) and not as a wave. I have deleted the code line corresponding to the x3 specification and now it works!

Duncan