title: looping through column data to fit curve in matlab
body: <p>I have a spreadsheet <code>ANA2009_1ag.xlsx</code> with column 1= wavelength lambda and col2=specta1, col3=spectra2, col4=spectra3 etc</p>
  
  <p>I want to fit a curve to each of the spectra using the equation:</p>
  
  <p><code>ag(lambda) = ag(lambda_o)*exp(-S(lambda-lambda_o)</code></p>
  
  <p>So I can get the slope <code>S</code>.</p>
  
  <p>I have the code and I am able to run it if I have a text file with 2 columns:
  column 1= wavelength lambda and col2=specta1
  But as i said above I have several spectra so i would like to make a loop so that the routine i got for fitting the curve and getting the slope loops from one column to the next.  ut as a novice in matlab loops are my biggest problem. 
  My code is as follows.</p>
  
  <pre><code>%//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  [num,txt,raw]=xlsread('ANA2009_1ag.xlsx');
  
  %// Get the size of data to be plotted 
  [r,c]=size(num);
  
  %//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   %// using txt vector to make appropriate legend that matches the data plot
     Sepctra=cellstr(txt); %//C = cellstr converts array to a cell array.
     %//FIND OUT HOW YOU CAN SAY TO END OF COLUMN 
  for i=2:7
     Sepctra(i)=txt(i);
  end   
  
  %//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  %//takeout range of interest
  I=find(num(:,1)&lt;650 &amp; num(:,1)&gt;400);
  
  wl=num(I,1);
  a_g=num(I,2);
  
  
  %//setting options for fmisearch
  opts = optimset('fminsearch');      
  opts = optimset(opts,'MaxIter',4000); 
  opts = optimset(opts,'MaxFunEvals',2000);   %// usually 100*number of params
  opts = optimset(opts,'TolFun',1e-9);
  %//opts = optimset('LevenbergMarquardt','on');
  
  %//guess for paramters (amplitude at 532 and slope)
  x0=[0.1, 0.03];
  
  %//minimization routine
  x1 = fminsearch(@least_squares,x0,opts,a_g,wl)
  
  %//plot data and fit
  plot(wl, a_g, '.k', wl, x1(1)*exp(-x1(2)*(wl-412)),'b')
  </code></pre>
  
  <p>DATA IS FROM 300 NM (LAMBDA) TO 685 NM (lambda) BUT I CHOSE TO FIT THE CURVE FROM 650-400NM</p>
  
  <pre><code>Wavelength  AN10070A-4m AN10066A-10m 
  300         1.561434    1.434769
  300.5       1.549919    1.42786 
  301         1.531495    1.414042
  301.5       1.506162    1.400224 
  302         1.483132    1.386406
  302.5       1.467011    1.372588 
  303         1.45089     1.356467
  303.5       1.443981    1.342649 
  304         1.42786     1.333437
  304.5       1.414042    1.324225 
  305         1.407133    1.31271
  </code></pre>
  
  <p>My expected output is: 
  x1 which is a vector of the amplitude at 412nm and the slope S
  I would like to post the figure that i got for the curve fit but I dont know how to do it. </p>