function Fc = cutoff(F,Txym)
%CUTOFF Create plot of datasets and fits
% CUTOFF(F,TXYM)
% Creates a plot, similar to the plot in the main curve fitting
% window, using the data that you provide as input. You can
% apply this function to the same data you used with cftool
% or with different data. You may want to edit the function to
% customize the code and this help message.
%
% Number of datasets: 1
% Number of fits: 1
% Copyright 2007 The MathWorks, Inc.
% Data from dataset "Txym vs. F":
% X = F:
% Y = Txym:
% Unweighted
%
% This function was automatically generated on 10-May-2006 15:16:56
% Set up figure to receive datasets and fits
f_ = clf;
figure(f_);
set(f_,'Units','Pixels');
legh_ = []; legt_ = {}; % handles and text for legend
xlim_ = [Inf -Inf]; % limits of x axis
ax_ = axes;
set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]);
set(ax_,'Box','on');
axes(ax_); hold on;
% --- Plot data originally in dataset "Txym vs. F"
F = F(:);
Txym = Txym(:);
h_ = line(F,Txym,'Parent',ax_,'Color',[0.333333 0 0.666667],...
'LineStyle','none', 'LineWidth',1,...
'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(F));
xlim_(2) = max(xlim_(2),max(F));
legh_(end+1) = h_;
legt_{end+1} = 'Txym vs. F';
% Nudge axis limits beyond data limits
if all(isfinite(xlim_))
xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
set(ax_,'XLim',xlim_)
end
% --- Create fit "fit 1"
% Apply exclusion rule "edges"
if length(F)~=8193
error('Exclusion rule "edges" is incompatible with F');
end
ex_ = logical(zeros(length(F),1));
ex_([[]]) = 1;
ex_ = ex_ | ~(200<F & F<18000);
fo_ = fitoptions('method','NonlinearLeastSquares','Lower',0);
ok_ = ~(isnan(F) | isnan(Txym));
st_ = [0.106 ];
set(fo_,'Startpoint',st_);
set(fo_,'Exclude',ex_(ok_));
ft_ = fittype('sqrt(1 + (2*pi*F).^2*CR^2)./(1 + (2*pi*F).^2*CR^2)',...
'dependent',{'T'},'independent',{'F'},...
'coefficients',{'CR'});
% Fit this model using new data
if sum(~ex_(ok_))<2 %% too many points excluded
error('Not enough data left to fit ''%s'' after applying exclusion rule ''%s''.','fit 1','edges')
else
[cf_,cf_goodness] = fit(F(ok_),Txym(ok_),ft_,fo_);
end
% Or use coefficients from the original fit:
if 0
cv_ = {8.676594150069e-005};
cf_ = cfit(ft_,cv_{:});
end
% Plot this fit
h_ = plot(cf_,'fit',0.95);
legend off; % turn off legend from plot method call
set(h_(1),'Color',[1 0 0],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
legh_(end+1) = h_(1);
legt_{end+1} = 'fit 1';
% Done plotting data and fits. Now finish up loose ends.
hold off;
h_ = legend(ax_,legh_,legt_,'Location','NorthEast');
set(h_,'Interpreter','none');
xlabel(ax_,''); % remove x label
ylabel(ax_,''); % remove y label
title(sprintf('R^2 = %g', cf_goodness.rsquare));
%% Additional Section:
% Compute and display cutoff frequency
CR = coeffvalues(cf_);
Fo = 1/(CR*2*pi);
vline(Fo,'r',['Cutoff = ' num2str(Fo,4) ' Hz'])
if nargout == 1
Fc = Fo;
end