Using CurveFit parameters in procedures

Hi,

I am trying to implement this procedure:
#pragma rtGlobals=3 // Use modern global access method and strict wave access.
Function myAuto()
Wave Vx,i_t
Display Vx vs i_t
CurveFit/M=2/W=0 line, Vx/X=t/D
Make/N=50000/D fit
fit= W_coef[0]+W_coef[1]* i_t
Vx = Vx - fit
Save/T i_t,Vx,Vy as "aaa.itx"
End



The problem is in W_coeff, as Igor think they are "unkown/inappropriate". How can I modify it?
You need to declare W_coef so that Igor knows what it is as follows:
Wave W_coef


For details execute:
DisplayHelpTopic "Accessing Global Variables And Waves"

Thanks, that did it.

Another question if possible. How to add a variable inside the wave name?

I want something like this:

for i = 001 to 100

... Vx_i ....

end for

i.e. I have lots of files that differs in name, Vx_001, Vx_002, ..., Vx_999 etc.

I know this should be easy in VB or C++ but not sure how it works in Igor.
You will need to create a list of wavenames that you wish to work on. Depending on how your data is organized, and if naming conventions are held between sample sets, will determine how you actually go about it. My code below will assume that you want to do the fit on all waves labeled as "Vx_####" and that there are no such waves starting with Vx_ to be excluded.

string list = wavelist("Vx_*", ";", "")   //get list of all waves, starting with Vx_ in CURRENT DATA FOLDER
wave i_t
variable i
for (i=0; i<itemsinlist; i+=1)
  wave w = $(stringfromlist(i, list, ";"))    //wave reference to i-th wave in list
  CurveFit/M=2/W=0 line, w/X=t/D
  //rest of code dealing with results
endfor


Since you will be dealing with a large number of waves, you will want to re-think how you store/save the results. You may not want to create a *.itx file for every single wave (or maybe you do).
Function Demo()
    Variable i
    for(i=0; i<100; i+=1)
        String name
        sprintf name, "Vx_%d", i
        Make /O /N=(50) /D $name
        Wave w = $name
        w = i  
    endfor
End


For details:
// Execute this for help
DisplayHelpTopic "Converting a String into a Reference Using $"

Hello,
Thanks for the responses.

@Proland, I got an error in the 2nd semicolon in the for loop statement, "expected left parenthesis".

Also, I must clarify that there exist no waves such as Vx_25, for example. What exist is Vx_025. This means that all the numbers should be formatted accordingly. In VB, this is done using

i = Format(i, String(3, "0"))


EDIT: of course I can use three loops plus some if statements, but if there's a ready formatting option it would be nice.

EDIT: I figured it out. I used

sprintf name1, "VT%03d_S1Vx_V", i
ali8 wrote:

@Proland, I got an error in the 2nd semicolon in the for loop statement, "expected left parenthesis".

Also, I must clarify that there exist no waves such as Vx_25, for example. What exist is Vx_025. This means that all the numbers should be formatted accordingly. In VB, this is done using



My bad, I was in a rush,

for (i=0; i<itemsinlist(list); i+=1)


Also, the formatting of the numbers doesn't matter in my case since it looks up the actual names of waves that exist.
Thanks for both of you, I did it correctly. I have another non related question which I'll post as a new thread.
Hello,

I was trying to save the waves automatically. I used:

Function BatchFit()
    Wave W_coef
    Variable i
    for(i=7; i<168; i+=1)
        String name5
        sprintf name5, "C:\Users\[username]\Desktop\VT%03d.itx",i
                (some code here)
        Save /T/M="\r\n" w2,w1,w4,w6,w7,w8 as name5
    endfor
End


but that did not work. The manual says there's a flag /P to be used as Path but did not work also.
Sorry, I just discovered how it is done, using the "new Path" command. It is strange why we need this but anyway thanks.
Hello again,

I am sorry for all of these questions. At least I am trying and I solved some of them myself.

But there's a serious problem.

The data being saved is not correct, or more precisely it shows (in notepad) as NaN.

Here's the code:

#pragma rtGlobals=3     // Use modern global access method and strict wave access.
Function BatchFit()
    Wave W_coef
    Variable i
    for(i=7; i<11; i+=1)
        String name1,name2,name3,myFileName
        sprintf name1, "VT%03d_S1Vx_V", i
        sprintf name2, "VT%03d_S1t_ms", i
        sprintf name3, "fit%d", i
        sprintf myFileName, "VT%03d.itx", i

        Wave w1=$name1
        Wave w2=$name2
        Wave w3=$name3

        Display w1 vs w2
        Make/N=50000/D $name3
        CurveFit/NTHR=0 poly 5,  w1 /X=w2 /D
        w3 = w_coef[0] + w_coef[1] *w2 + w_coef[2] *w2*w2 + w_coef[3] *w2*w2*w2 + w_coef[4] *w2*w2*w2*w2
        w1 = w1 - w3
        Save /P=mypath_fit /T/M="\r\n" w2,w1 as myFileName
    endfor
End


Here w1 is shown (in notepad) as NaN, while w2 is intact. What is the problem? When I redo exactly the same steps without the procedure (i.e. manually) it works well.
A wave declaration statement must appear after the wave is created. So the statement:
Wave w3=$name3

should be after
Make $name3


Also make sure that the debugger is turned on:
DisplayHelpTopic "The Debugger"

and that "Debugging on Error" and "NVAR SVAR WAVE Checking" are turned on.

Set a breakpoint in the function and use the debugger to examine the various wave references and the waves to which they point.
After debugging I can see the problem is in the W_coef, they are all Null.

I don't know why, although the W_coef wave generated by Igor is OK. All I have done is to take its elements and use them in my code, not sure why it does not work.

Specifically, the debugger shows (on top) the following: a wave read error: "Attempt to operate on a null (missing) wave"
That's because w_coef isn't guaranteed to exist at the time "Wave w_coef" is excecuted. This line must occur after the CurveFit to ensure it's existence.
That's correct, Proland!

Thanks a lot. I am grateful for you and hrodstein.