write and run a script that loads waves from files and rename them accordingly

Hello,
I have N data files data.igr (in Igor Text Data format) each of which is contained into a folder DATA_MAIN/run1 ... DATA_MAIN/runN.

What I actually do is that I copy-paste over and over the following script into my command window:
//initialisation (just run once) •numrun=-1 •string strangle //LOAD DATA MANUALLY ? // to be copy-paste several times into the command line •numrun+=1 •strangle=num2str(angle[numrun]) •strnom="_theta"+strangle •strname="sz"+strnom •duplicate/O sz $strname •strname="sv"+strnom •duplicate/O svx $strname •FuncFit/X=1/H="001"/NTHR=0/TBOX=1023 MyFun W_coef svx/X=sz /D /F={0.950000, 4} // (I don't know how to use strings here, as there is the H="001" argument in this command) •AA[numrun]=W_coef[0] •BB[numrun]=W_coef[1] •CC[numrun]=W_coef[2]

Do you know if there is another better way to do so?
In particular, how can I make a loop on loading the files? Also, is it possible to write and run a script? I tried using function, but I don't know how to properly code functions without putting all the waves I am working on, within the function's argument, which is a pain.

Thank you for your help
This can probably get you started. It creates a menu item to run your function from, and will load all ".igr" files (assumed to be Igor text files) located in the subfolders of the folder selected by the user.

//  Creates a simple menu for running the Test function with the click of the mouse
Menu "DoStuff"
    "Run Test", /Q, Test()
end


Function Test()

    //  Allows the user to select a folder to load
    NewPath/M="Load all waves in folder" /O/Q MainFolderPath
   
    //  Returns a semicolon separated string of all subfolders in MainFolderPath
    String AllSubFolders=IndexedDir(MainFolderPath, -1, 1)
   
    //  Finds the number of subfolders
    Variable NumberOfSubfolders=ItemsInList(AllSubFolders, ";")
   
    //  Counts through the subfolders
    Variable i=0, ii=0, NumberOfFiles=0
    String ActiveSubFolder="", AllFilesInFolder="", ActiveFileName=""
    for (i=0; i<NumberOfSubFolders; i+=1)
   
        //  Finds the name of the next subfolder
        ActiveSubFolder=StringFromList(i, AllSubFolders, ";")
       
        //  Creates a path to ActiveSubFolder
        NewPath /O/Q SubFolderPath, ActiveSubFolder
       
        //  Returns a semicolon separated string of all .igr files in ActiveSubFolder
        AllFilesInFolder=IndexedFile(SubFolderPath, -1, ".igr")
       
        //  Finds the number of files in ActiveSubFolder
        NumberOfFiles=ItemsInList(AllFilesInFolder, ";")
       
        //  Counts through the files in ActiveSubFolder
        for (ii=0; ii<NumberOfFiles; ii+=1)
       
            //  Finds the name of the next file
            ActiveFileName=StringFromList(ii, AllFilesInFolder, ";")
           
            //  Loads the file as an Igor text file. You probably have to adjust this to work with your files.
            LoadWave/O/P=SubFolderPath /Q/T ActiveFileName
        endfor
    endfor
end
[quote=hrodstein]Execute:
DisplayHelpTopic "Loading Waves Using Igor Procedures"


Also, search IgorExchange code snippets for examples: http://www.igorexchange.com/search/node/LoadWave+type%3Acode_snippet[/q…]

Great, thanks. I think I am getting closer to my goal.
But how to automatically fit using a changing name for the wave to be fit? The fit function argument H="001" with the quotes makes it impossible for me to create and changing string that I  execute

Thank you for your support
You pretty much never want to use Execute. As I understand it Execute is a relic left over from ancient versions of Igor. Execute still has a few uses, but they are few and far in between; you want to use procedures and functions.

Assuming you have for instance a semicolon separated list of waves like the one returned by WaveList you could do something like:
for (i=0; i<n; i+=1)
     Wave MyWave=$StringFromList(i, ListOfWaves, ";")
     FuncFit MyWave
endfor


To write an " in a string use \". That means to give a string the value: FuncFit H="001" you need to write String MyString="FuncFit H=\"001\""
tijead wrote:
But how to automatically fit using a changing name for the wave to be fit?


Execute this:
DisplayHelpTopic "Converting a String into a Reference Using $"


and this:
DisplayHelpTopic "Accessing Global Variables And Waves"

hrodstein wrote:
tijead wrote:
But how to automatically fit using a changing name for the wave to be fit?


Execute this:
DisplayHelpTopic "Converting a String into a Reference Using $"


and this:
DisplayHelpTopic "Accessing Global Variables And Waves"


Thanks! now I've got it ;)
Here is my code:
function FitIndiv(NomRun,fitDi,Lrelax,muc,bbeta,flag_muc,flag_beta,flag_ell)
    variable muc,bbeta,flag_muc,flag_beta,flag_ell
    wave NomRun,fitDi,Lrelax
    wave NameRun  = root:NameRun
    wave ucenter    = root:ucenter
    wave centercell = root:centercell
    wave mu           = root:mu
   
    Make/O/N=(numpnts(NomRun)) fitUc,fitbeta
   
    string strfix
    wave W_coef
   
    strfix="010"+num2str(flag_muc)+"1"+num2str(flag_beta)+num2str(flag_ell)
    display
   
    variable i
    for (i=0;i<numpnts(NomRun);i+=1)
        variable pointeur
        FindValue /S=0/T=1e-3 /V=(NomRun[i]) NameRun; pointeur=V_value;
        W_coef={ucenter[pointeur],centercell[pointeur],fitDi[i],muc,mu[pointeur],bbeta,Lrelax[i]}// init Guess
        string phrase
        phrase="FuncFit/X=1/H=\""+strfix+"\"/NTHR=0/TBOX=1023 uNLsh W_coef  cnvx"+num2str(NomRun[i])+" /X=cnsy"+num2str(NomRun[i])+" /D"// /F={0.950000, 4}"
        print "------- Run "+num2str(NomRun[i])+" -------"
        print W_coef
        print phrase
        execute phrase
       
        // Plot
        appendtograph $("cnvx"+num2str(NomRun[i])) vs $("cnsy"+num2str(NomRun[i]))
        appendtograph $("fit_cnvx"+num2str(NomRun[i]))
        string str=("ModifyGraph lsize(cnvx"+num2str(NomRun[i])+")=2,lstyle(fit_cnvx"+num2str(NomRun[i])+")=7,lsize(fit_cnvx"+num2str(NomRun[i])+")=3;DelayUpdate")
        execute str
        str="ModifyGraph rgb(fit_cnvx"+num2str(NomRun[i])+")=(16385,28398,65535)"
        execute str
        string boxstr="\\Z24run"+num2str(NomRun[i])
        TextBox/C/N=text0/A=MC boxstr
        DoUpdate
        sleep/s 0.5
       
        //Save data
        fitUc[i]=W_coef[0]
        fitDi[i]=W_coef[2]
        fitbeta[i]=W_coef[5]
    endfor
End
You can replace
string str=("ModifyGraph lsize(cnvx"+num2str(NomRun[i])+")=2,lstyle(fit_cnvx"+num2str(NomRun[i])+")=7,lsize(fit_cnvx"+num2str(NomRun[i])+")=3;DelayUpdate")
execute str

with
 ModifyGraph lsize($("cnvx"+num2str(NomRun[i])))=2, lstyle($("fit_cnvx"+num2str(NomRun[i])))=7, lsize($("fit_cnvx"+num2str(NomRun[i])))=3

You can do the same for the FuncFit line.
I assume you use DoUpdate and Sleep to turn the updates into an animation of sorts?