Run a function from a macro

Dear Igor Community,

I am trying to integrate a function inside a macro (kindly see below). My question: How can I call a function with my macro? I tried the "execute" option but it is not working.

Second option is to basically take the code of the function and integrate it inside the macro but I keep getting syntax error as the function is written in a way that is not macro friendly (I wrote the macro but did not write the function myself)

Could you help please? the function is called "Function KillAllGraphs()"
 

Macro poolanalysisfixFinal(EndStr)
String Endstr="sub"
prompt Endstr, "Enter match string here"
string  traceslist, currenttracename, dep, depolarization
traceslist = Wavelist("*"+Endstr,";","")
variable traceindex=0
do
    currenttracename = StringFromList (traceindex, traceslist,  ";")
    if (strlen(currenttracename) == 0)
        break
    endif
    dep=currenttracename+"dep"
    duplicate/O $currenttracename $dep
    depolarization=currenttracename+"_T"

 
    Make/O/N=11 $depolarization
    $depolarization[0] = mean($dep,pnt2x($dep,5),pnt2x($dep,302)) //baseline   
    $depolarization[1] = mean($dep,pnt2x($dep,328),pnt2x($dep,625)) //first depolarization
    $depolarization[2] = mean($dep,pnt2x($dep,651),pnt2x($dep,948))  //second depolarization
    $depolarization[3] = mean($dep,pnt2x($dep,974),pnt2x($dep,1271))  //third depolarization
    $depolarization[4] = mean($dep,pnt2x($dep,1297),pnt2x($dep,1594)) //fourth depolarization
    $depolarization[5] = mean($dep,pnt2x($dep,1620),pnt2x($dep,1917)) //fifth depolarization
    $depolarization[6] = mean($dep,pnt2x($dep,1943),pnt2x($dep,2240))  //sixth depolarization
    $depolarization[7] = mean($dep,pnt2x($dep,2359),pnt2x($dep,2656))  //seventh depolarization
    $depolarization[8] = mean($dep,pnt2x($dep,2775),pnt2x($dep,3072))  //eighth depolarization
    $depolarization[9] = mean($dep,pnt2x($dep,3191),pnt2x($dep,3488))  //ninth depolarization
    $depolarization[10] = mean($dep,pnt2x($dep,3607),pnt2x($dep,3904))  //tenth depolarization

 
//$depolarization[0]=0
$depolarization[10] -= $depolarization[9]
$depolarization[9] -= $depolarization[8]
$depolarization[8] -= $depolarization[7]
$depolarization[7] -= $depolarization[6]
$depolarization[6] -= $depolarization[5]
$depolarization[5] -= $depolarization[4]
$depolarization[4] -= $depolarization[3]
$depolarization[3] -= $depolarization[2]
$depolarization[2] -= $depolarization[1]
$depolarization[1] -= $depolarization[0]

 
traceindex+=1
    print "traceindex = ",traceindex
    print currenttracename
    display $depolarization
while(1)

 
Function KillAllGraphs()  
   string fulllist = WinList("*", ";","WIN:1")
    string name, cmd
    variable i
    for(i=0; i<itemsinlist(fulllist); i +=1)
        name= stringfromlist(i, fulllist)
        sprintf  cmd, "Dowindow/K %s", name
        execute cmd    
    endfor
end
endmacro

 

Wouldn't it be the more elegant solution if the function is called as a function and not as a macro? At the same time, a function could then be called that is not suitable for macros.

In functions you can also enable user input using doprompt:

function poolanalysisfixFinal()
    string Endstr
    prompt Endstr, "sub"
    string s_promt="Enter match string here"
    doprompt s_promt, Endstr
    ...

I have no idea what this function is doing, but maybe something like this could work?

function poolanalysisfixFinal()
    string Endstr
    prompt Endstr, "sub"
    string s_promt="Enter match string here"
    doprompt s_promt, Endstr
    string  traceslist, currenttracename, dep, depolarization
    traceslist = Wavelist("*"+Endstr,";","")
    variable traceindex=0
    do
        currenttracename = StringFromList (traceindex, traceslist,  ";")
        if (strlen(currenttracename) == 0)
            break
        endif
        dep=currenttracename+"dep"
        duplicate/O $currenttracename $dep
        depolarization=currenttracename+"_T"

 
        Make/FREE/O/N=11 w_depolarization
        w_depolarization[0] = mean($dep,pnt2x($dep,5),pnt2x($dep,302)) //baseline    
        w_depolarization[1] = mean($dep,pnt2x($dep,328),pnt2x($dep,625)) //first depolarization
        w_depolarization[2] = mean($dep,pnt2x($dep,651),pnt2x($dep,948))  //second depolarization
        w_depolarization[3] = mean($dep,pnt2x($dep,974),pnt2x($dep,1271))  //third depolarization
        w_depolarization[4] = mean($dep,pnt2x($dep,1297),pnt2x($dep,1594)) //fourth depolarization
        w_depolarization[5] = mean($dep,pnt2x($dep,1620),pnt2x($dep,1917)) //fifth depolarization
        w_depolarization[6] = mean($dep,pnt2x($dep,1943),pnt2x($dep,2240))  //sixth depolarization
        w_depolarization[7] = mean($dep,pnt2x($dep,2359),pnt2x($dep,2656))  //seventh depolarization
        w_depolarization[8] = mean($dep,pnt2x($dep,2775),pnt2x($dep,3072))  //eighth depolarization
        w_depolarization[9] = mean($dep,pnt2x($dep,3191),pnt2x($dep,3488))  //ninth depolarization
        w_depolarization[10] = mean($dep,pnt2x($dep,3607),pnt2x($dep,3904))  //tenth depolarization

 
        //$depolarization[0]=0
        w_depolarization[10] -= w_depolarization[9]
        w_depolarization[9] -= w_depolarization[8]
        w_depolarization[8] -= w_depolarization[7]
        w_depolarization[7] -= w_depolarization[6]
        w_depolarization[6] -= w_depolarization[5]
        w_depolarization[5] -= w_depolarization[4]
        w_depolarization[4] -= w_depolarization[3]
        w_depolarization[3] -= w_depolarization[2]
        w_depolarization[2] -= w_depolarization[1]
        w_depolarization[1] -= w_depolarization[0]
        duplicate/O w_depolarization $depolarization
 
    traceindex+=1
        print "traceindex = ",traceindex
        print currenttracename
        display $depolarization
    while(1)
    KillAllGraphs()
end

Function KillAllGraphs()  
   string fulllist = WinList("*", ";","WIN:1")
    string name, cmd
    variable i
    for(i=0; i<itemsinlist(fulllist); i +=1)
        name= stringfromlist(i, fulllist)
        sprintf  cmd, "Dowindow/K %s", name
        execute cmd    
    endfor
end

 

In reply to by g.germer

Thank you very much for the feedback. The macro is picking ibws from the data browser and it starts analyzing certain points. 

I wanted it to a be a fully automated macro because I am still on my way to add more modifications. I wanted to integrate the last function so that at the end of the analysis it closes all the windows/graphs that have been plotted so that I do not accumulate lots of unwanted figures. I found this small function on the forum and it works great independently when I run it outside the macro. I wanted actually to put a command inside the macro to basically "run" the function 

 

I usually go manually to the cmd line and write "Killallgraphs()" .. now I want to tell Igor to do that for me inside the macro, do you know if this is possible? 

What happens if you write the function KillAllGraphs() as a separate function and execute it within the macro?

Macro poolanalysisfixFinal(EndStr)
...
while(1)

KillAllGraphs()

endmacro

 
Function KillAllGraphs()  
   string fulllist = WinList("*", ";","WIN:1")
    string name, cmd
    variable i
    for(i=0; i<itemsinlist(fulllist); i +=1)
        name= stringfromlist(i, fulllist)
        sprintf  cmd, "Dowindow/K %s", name
        execute cmd    
    endfor
end

Alternative: Maybe you can use something like this?

Function KillAllGraphs()  
   string fulllist = WinList("*", ";","WIN:1")
   variable i
   for(i=0; i<itemsinlist(fulllist); i ++)
       killwindow $StringFromList(i,fulllist)
   endfor
end

 

We strongly encourage you to write your code as functions instead of macros. Is there any particular reason for making it a macro? I know new users tend to prefer macros because it is more like using the command line, which makes it more accessible. But functions have a lot of advantages, including much faster execution and more advanced syntax options.

It can take some mind-bending to wrap your head around the need for wave references. Some reading (especially the last one):

DisplayHelpTopic "Local Versus Global Variables"

DisplayHelpTopic "Converting a String into a Reference Using $"

DisplayHelpTopic "Accessing Waves In Functions"

I'm also still a bit puzzled with the macro: Why are the waves displayed at all and then these windows are closed?
Is it possible to see them at all before they are closed with KillAllGraphs()?

If you would use

...
display/N=poolanalysisfixFinal $depolarization
...

to display the graphs, you could use KillImportGraphs()

Function KillImportGraphs()  
   string fulllist = WinList("poolanalysisfixFinal*", ";","WIN:1")
   variable i
   for(i=0; i<itemsinlist(fulllist); i ++)
       killwindow $StringFromList(i,fulllist)
   endfor
end

instead of the KillAllGraphs() function. In this case only your graphs will be killed and not every graph of the experiment.