Do loop in Macro

Dear All,
In a macro I want to create a certain number ("points") of waves. Every wave has to contain the data corresponding to a gaussian peak (see the script).
As you can see in the script, every gaussian peak (so every wave) is centered at 40, while I want the peaks to be centered at the values contained in a wave0 already present in the main folder (it is given by the instrument, I work with X-rays). The wave0 has the same number of points as the number of the gaussian waves. I am not able to implement it with the do cycle, somebody can help me?

Many thanks in advance for your suggestions.


macro Simulation(points,FWHM,theta)
    variable points
        prompt points "POINT number"
    variable FWHM
        prompt FWHM "peak FWHM"
    variable theta
        prompt theta "Degree of BG"

variable i
string peak

do
    peak = "peak" + num2str(i)
    Make/O/N=161 $peak
    $peak =  0.049 + 0.95*exp(-((BE_scale - 40)/(0.6024*(FWHM)))^2) + ((theta)*((BE_scale - 40)/(1+abs(BE_scale - 40))))
    i+=1
while(i<points)

end macro

We recommend all code be functions these days:
Function SimulationUI()

    Variable points
    Variable FWHM
    Variable theta
        prompt points "POINT number"
        prompt FWHM "peak FWHM"
        prompt theta "Degree of BG"

    DoPrompt "Make Gaussians", points, FWHM, theta
    if (V_flag == 0)
        Simulation(points,FWHM,theta)
    endif
end


Function Simulation(points,FWHM,theta)
    variable points
    variable FWHM
    variable theta
 
    variable i
    string peak
   
    WAVE wave0
    WAVE BE_scale       // I'm guessing that this is a wave because of the way it's used below
 
    for (i = 0; i<points; i+=1)
        peak = "peak" + num2str(i)
        Make/O/N=161 $peak/WAVE=wout
        Variable x0 = wave0[i]
        wout =  0.049 + 0.95*exp(-((BE_scale - x0)/(0.6024*(FWHM)))^2) + ((theta)*((BE_scale - x0)/(1+abs(BE_scale - x0))))
    endfor
end


John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Thanks a lot John! It works!

Only another question: I am going through the other items in the forum and I see that, like you, many experts suggest the use of user defined functions instead of macros. Is it because functions can work faster and more efficient than macros?

Again thanks for your help.
I see.....thanks for the explanation! Actually, I am much more familiar with Macros, but in the future I will try to convert them into user defined functions then!

Thanks again for the great help,

Have a nice WE.
johnweeks wrote:
We recommend all code be functions these days:


I would love to do that, but for creating complex GUIs, the window recreation macros are so much more practical than hand editing code in functions.
thomas_braun wrote:
I would love to do that, but for creating complex GUIs, the window recreation macros are so much more practical than hand editing code in functions.

And, in fact, Igor generates macros for that purpose.

But I find that when putting together a complex UI using a control panel, a recreation macro rarely captures the complexity I need. I usually mock up the panel in draw mode, generate a recreation macro, and when I'm satisfied, I convert the macro to a function and add the complexity I need. But I agree that it can be excruciating when you figure out later that you want the controls to be in different places!

However, check out Jim's procedure to help with problems like that:

#include <Rewrite Controls Position>

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com