Help with $ to call a wave

Hello,

I am trying to write procedures to fit force curves measured with AFM. I have already wrote 2 functions, one to load the force curves, and the second one to fit and subtract the base line. I would like now to write another function to fit the constant compliance region. In a nutshell, I use FindLevel to find the subrange of the wave to fit and then I do the fitting. The FindLevel works, but the fitting doesn't (return a null wave as W_coef). I guess the problem is in the way I call the X wave in the fitting (dis_V_app below), which is called using $ from a wavelist. I called the wave as wave/T, which is the only way as not to have errors during compile, but I am not sure it is the actually correct way. Here below is the function, if someone has some suggestion it would be highly appreciated.
Function InvOLS_correction (startV, endV)
   
    variable startV, endV
   
 // Make a new wavelist with the baseline corrected waves
   
    SetDataFolder Root:FC_BLcorrected
   
    string InvOLSList = wavelist("*",";","")
    NewDataFolder/O/S Root:FC_InvOLScorrected
    Make/O/T/N=(itemsinlist(InvOLSList)) BLcorrectedFC
    Wave_From_String (InvOLSList, BLcorrectedFC)
    DeletePoints 0,1, BLcorrectedFC
    DeletePoints 1,2, BLcorrectedFC
   
    variable i=0
    variable j=0
    variable k=0   
    wave/T RawFC
   
    for (i=0; i<numpnts(BLcorrectedFC); i=i+2)
   
    Duplicate/O  $("Root:FC_BLcorrected:"+BLcorrectedFC[i]), $("Defl_V_App_CC"+num2str(j))
   
    wave defl_V_App_CC = $("Defl_V_App_CC"+num2str(j))
    wave dis_V_app =$("Root:Force_Curves:"+RawFC[k+2])
    wave W_coef
   
    variable V_LevelX
   
    // transform the input value in volt in the corresponding x (position in the wave of the y value)
   
    FindLevel/P/Q Defl_V_app_CC, startV
    variable startV_app=floor(V_levelX)
    print startV_app
   
    FindLevel/P/Q Defl_V_app_CC, endV
    variable endV_app=floor(V_levelX)
    print endV_app
   
    CurveFit/NTHR=0 line  defl_V_App_CC[startV_app, endV_app] /X=dis_V_app/D
   
    print W_Coef
       
    j=j+1
    k=k+2
   
    endfor
   
   
End function</span>
... Now that I posted it here, I see that the variable k is not highlighted the same way.. could this be the problem?
AFAIK k is allowed as a variable. It's just not highlighted here, so I don't think that's your problem. I can't see what's wrong immediately. My guess is that you have wave scaling on the waves so that when you find V_LevelX this does not equal the point number for your curve fit. What I mean is that the Curvefit command is getting start and end points from startV_app and endV_app, but you are assigning an X value. X may not equal p in your case.
Hello, thanks for your help. I actually sorted the waves and the fisrt 10-20 points are empty.. could it be a problem? Otherwise, how can I make x and p match? thanks again, V.
.. I forgot to mention that if I do the fitting manually it works, meaning by selecting in the curve fit dialog windows the start and end points manually and the x wave that I want, then the fitting works
.. I forgot to mention that if I do the fitting manually it works, meaning by selecting in the curve fit dialog windows the start and end points manually and the x wave that I want, then the fitting works
Hi, Check the Variable V_Levelx declaration. It may confuse the Findlevel operation which creates its own V_Levelx. Try commenting it out. Andy
Hello,

thanks for your answer. I tryed your suggestion but nothing changes. I suspect that the problem is how I call the X wave of the fitting, because if I write it explicitly the fitting works (of course only for the wave I declared, meaning wave with the index 10 out of the group of waves I want to fit):
wave defl_V_App_CC10
wave dis_V_app10   
CurveFit/NTHR=0 line  Defl_V_App_CC10[startV_app, endV_app] /X=::Force_Curves:Dis_V_App10 /D
Curious are the waves in the curvefit the same number of points? You are taking a subset in the y wave, but the full wave as x. Andy
It's true, I take only a subrange of y and the whole x, but then if this was the problem why would it work if I do it manually? (... I am a total beginner, so most of the things I do is to do sth manually and then copy the command line in the procedure windows)
You use rawFC to define the x-wave for the fit. Is it in the correct data folder since you switch to the Root:FC_BLcorrected early in the function. However then you make a new data folder with over write and set NewDataFolder/O/S Root:FC_InvOLScorrected. I think that folder is now empty and then you use the wave reference for rawFC out of that folder. Is that correct? Andy .
I think that the initial setdatafolder is actually useless, as the folder is already selected after the function I launch before this one and indeed I just tried commenting it out and nothing changes. As for RawFC, it is located in the folder FC_BLcorrected, but the waves it refers to are in another folder, named Force_Curves. I thought that by declaring it this way it would work:
 wave dis_V_app =$("Root:Force_Curves:"+RawFC[k+2])
I also tried to declare RawFC at the beginning with the whole path:
 wave/T Root:FC_BLcorrected:RawFC
instead of
 wave/T RawFC
But the compiler tells me that the second : is a mistake