i want my compare function to only estimate fit for a certain range in matlab

You can modify the compare function to fit a certain range by modifying the data inputs to the function. Here's an example of how you might modify the compare function to estimate fit for data within a specified range:

main.m
function [fit, gof] = compare_range(xdata, ydata, fittype, xlimits)
%COMPARE_RANGE Compare data to a fit for a specified range
%   [FIT, GOF] = COMPARE_RANGE(XDATA, YDATA, FITTYPE, XLIMITS) compares the
%   data in XDATA and YDATA to a fit of the type specified by FITTYPE. The
%   function returns the fitted data (FIT) and the goodness-of-fit metrics
%   (GOF). The fit is only estimated for data in XDATA within the range
%   specified by XLIMITS.

% Subset data within specified range
rangeIdx = xdata >= xlimits(1) & xdata <= xlimits(2);
xdata = xdata(rangeIdx);
ydata = ydata(rangeIdx);

% Perform fit
[fit, gof] = fit(xdata, ydata, fittype);
649 chars
16 lines

In this modified function, you pass in xlimits, a vector with the minimum and maximum values for the range of data for which you want to estimate fit. The function subsets the data within this range before performing the fit.

gistlibby LogSnag