function [c,g]=do_fit(bins,aux) c={}; c.f=nan; g=nan; if ~any(isnan(aux)) sel=bins>0.015 & bins<0.3; fo = fitoptions('Method','NonlinearLeastSquares',... 'Lower',[0.2,0,0,0,6,-1,-1],... 'Upper',[2,20,1,Inf,12,1,1],... 'StartPoint',[1,3,.2,1,8,0,0]); ft = fittype('a*exp(-b*x)+c*cos(2*pi*f*x)*exp(-d*x)+g+h*x','options',fo); [c, g] = fit(bins(sel)',aux(sel)',ft); end