Help for baseline fit and subtract

Hello,

I am completely new to programming and I just started to learn Igor in order to write a procedures to process AFM force curves. Once the force curves are loaded (4 waves for each force curves, two y and two x), I want to fit a baseline on a subrange of the y waves and then subtract it. I created a wave where I list all the waves and then I go though it and I fit ones I want to fit (the first 2 every four, which are the y waves).
This is what I wrote:

Function Baseline_correction ()
   
string BaselineList = wavelist("*",";","")
    Make/T/N=(itemsinlist(BaselineList)) RawFC
    Wave_From_String (BaselineList, RawFC)
   
    variable i=0
    variable j=0
    for (i=0; i<numpnts(Root:Force_Curves:RawFC); i=i+4)
   
    CurveFit/Q/NTHR=0 line  $(RawFC[i])[30,100] /X=$(RawFC[i+2]) /D
   
    CurveFit/Q/NTHR=0 line  $(RawFC[i+1])[30,300] /X=$(RawFC[i+3]) /D
   
    endfor
end function


Till here everything is fine, the curve fitting is done and a fit_wave wave is created for each of them. At this point though I am unable to subtract the fitting. I though about writing under the fitting command CurveFit this:
wave W_coef
wave blfit
blfit = W_coef[0] + W_coef[1]*x
blfit -= $(RawFC[i])

But it doesn't work. I also tried this:
$RawFC[i+1]=$RawFC[i+1]-(W_coef[0]+W_coef[1]*$RawFC[i+3])

But again, It doesn't work. I get a message saying the $ cannot used this way (but even if I take it out there is another error).

If someone has any suggestion it would be really appreciated !!

Thanks in advance
Hello,
thanks for your help. I tried to do as you suggested, but it doesn't do what I wanted to - maybe I didnt explain myself well, what I would like to do is:
CurveFit WaveName
WaveName=WaveName-fit_WaveName


or

CurveFit WaveNameX=WaveNameY
WaveNameX=WaveNameX-(W_coef[0]+W_coef[1]WavenameY)


The former works if I type it in the command line, but I don't know how to insert it into the procedure as to do it automatically to all my waves.

Thanks again.
If you use the /R flag in CurveFit, the residual wave wiill contain NaNs outside of the fit range, which is probably why that method doesn't work for you.

Vivvi87 wrote:

wave W_coef
wave blfit
blfit = W_coef[0] + W_coef[1]*x
blfit -= $(RawFC[i])


wave W_coef
wave blfit
wave w=$RawFC[i]
blfit = W_coef[0] + W_coef[1]*x
blfit = w-blfit