Displaying FuncFit in a loop

I'm trying to do a fit to several subranges of my wave. I have put it in a loop and fit converges properly. Within the same loop I'm trying to Display each subrange of my wave with the fit fuction calculated on a separate plot. But the fit function that is displayed on all of the plots is the same (the last calculated). As I'm new at IgorPro I don't understand why it is happening and how does FuncFit save the data.
All I want is to get a batch of graphs where a single subrange of data and a fit for it whould be on a one separate graph. Can you please help me? Here is my code:

Function myFit(w,x)
    Wave w
    Variable x
 
    Variable bm= w[0]                      
    Variable kd= w[1]                      
 
    return w[0]*x/ (w[1]+x)    
End


Function Ex (m)
         Variable m
         Variable n, a,i,s

           a=0
           n=0
           Make/O/N = (61)kd_global
         
        do
            Display DNAbound_Protein[n,n+11] vs DNAfree[n,n+11]
            ModifyGraph mode=3, marker=19
            SetAxis left 0,1.2
            SetAxis bottom 0, 35000
            label bottom "(DNAfree)"
            label left "(DNAbound/Protein)"
   
              Make/D/O/N=2 W_coef
              W_coef= {1.2,1}
              FuncFit/H="10"/NTHR=0/Q/TBOX=64 myFit W_coef DNAbound_Protein[n,n+11] /X=DNAfree[n,n+11]/D
           
              kd_global[a]= (W_coef[1]+477.1)/37.379
         
             n+=12
             a+=1
      while (n < m)  

     Display  kd_global
           

      AppendToTable /W=Table0 kd_global
 
End
This happens, because your fit wave is overwritten in each iteration of the loop. You can duplicate the generated fit wave in each iteration to avoid the problem.
Just write after the funcfit command:
duplicate/o fit_DNAbound_Protein, $("fit"+num2istr(a)+"_"+"DNAbound_Protein")
And if you want to display the fits together with your data add the following:
display fit_DNAbound_Protein, $("fit"+num2istr(a)+"_"+"DNAbound_Protein")
wave w=$("fit"+num2istr(a)+"_"+"DNAbound_Protein")
setaxis bottom, leftx(w),rightx(w)-deltax(w)

Andreas
As awirsing said, FuncFit overwrites it's own resultwave.
This is indicated by the parameter /D at the end of FuncFit (look into the help of 'CurveFit') which just writes the result in a wave named 'fit_' + inputname. Since the Wave you are fitting has the same name (only the subrange is different) the result has the same name, too. I think to solve your problem you can just try to define a new output name with /D='newname' (e.g. "FitResult"+num2str(n) ) every iteration, so you don't even have to duplicate waves.
The comments by chozo and awirsing are both correct. I'd just like to add that using /D= has the further consequence that the destination wave must have the same number of points as your Y data wave, which may or may not result in a good-looking graph trace.

It has the possible advantage that you can do each of your fits using the same destination wave. You could write:

Duplicate DNAbound_Protein, DNAbound_Protein_Fit
do
    ...
    FuncFit ... DNAbound_Protein[n,n+11] /X=DNAfree[n,n+11]/D= DNAbound_Protein_Fit[n,n+11]
    ...
while ...


John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Dear chozo, awirsing and johnweeks, thank you very much! That was something I could not get to for weeks.
isakova wrote:
Dear chozo, awirsing and johnweeks, thank you very much! That was something I could not get to for weeks.

Everything seem to be correct and makes sense. But still does not work properly. Mainly the plotting part.
when I'm trying to add lines
display fit_DNAbound_Protein, $("fit"+num2istr(a)+"_"+"DNAbound_Protein")
wave w=$("fit"+num2istr(a)+"_"+"DNAbound_Protein")
setaxis bottom, leftx(w),rightx(w)-deltax(w)

as chozo suggested, it displays the right fit but with the another fit (fit_DNAbound_Protein). and then I've tried to modify it on:
  display DNAbound_Protein[n,n+11], $("fit"+num2istr(a)+"_"+"DNAbound_Protein")

it displays the fit_num2str(a) properly but puts all the the data points (DNAbound_Protein[n,n+11]) at x=0 and not as it should be at x=DNAfree[n,n+11].
Can I modify it somehow to work properly?

Thank you,
Alina.
I looked at your code more carefully than the first time...

The problem is probably related to the SetAxis commands. Unfortunaltey I don't know how your data look like, so I can't test it, but please give the following a try:
Function Ex (m)
         Variable m
         Variable n, a,i,s
 
           a=0
           n=0
           Make/O/N = (61)kd_global
 
        do
            Display DNAbound_Protein[n,n+11] vs DNAfree[n,n+11]
            ModifyGraph mode=3, marker=19
//            SetAxis left 0,1.2
//            SetAxis bottom 0, 35000
            label bottom "(DNAfree)"
            label left "(DNAbound/Protein)"
 
              Make/D/O/N=2 W_coef
              W_coef= {1.2,1}
              FuncFit/H="10"/NTHR=0/Q/TBOX=64 myFit W_coef DNAbound_Protein[n,n+11] /X=DNAfree[n,n+11]/D
              wave w= fit_DNAbound_Protein
              duplicate/o w $("fit"+num2istr(a)+"_"+"DNAbound_Protein")
              wave w1=$("fit"+num2istr(a)+"_"+"DNAbound_Protein")
              appendToGraph w1
              removeFromGraph/z $nameofwave(w)
 
              kd_global[a]= (W_coef[1]+477.1)/37.379
 
             n+=12
             a+=1
      while (n < m)  
 
     Display  kd_global
 
 
      AppendToTable /W=Table0 kd_global
 
End


As I do not know what the reason is for the original SetAxis commands, I commentized them. You can reactivate them if you know what you're doing.

I hope this helps,
Andreas
Hi Andreas,

Your corrected code works just perfect!
Even uncommented SetAxis does not change the correctness of the plots.
Thank you.

Alina