Code covered by the BSD License  

Highlights from
MATLAB Tools for Scientists – Introduction to Statistical Analysis

image thumbnail

MATLAB Tools for Scientists – Introduction to Statistical Analysis

by

 

04 Feb 2011 (Updated )

Examples used in the webinar: MATLAB Tools for Scientists – Introduction to Statistical Analysis

createFit(x,y,x1,y1)
function [cf_1, cf_2] = createFit(x,y,x1,y1)
%CREATEFIT Create plot of data sets and fits
%   CREATEFIT(X,Y,X1,Y1)
%   Creates a plot, similar to the plot in the main Curve Fitting Tool,
%   using the data that you provide as input.  You can
%   use this function with 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 data sets:  2
%   Number of fits:  2

% Data from data set "y vs. x":
%     X = x:
%     Y = y:
%     Unweighted

% Data from data set "y1 vs. x1":
%     X = x1:
%     Y = y1:
%     Unweighted

% Auto-generated by MATLAB on 15-Sep-2010 16:44:55
% Copyright 2010 - 2011 MathWorks, Inc.

% Set up figure to receive data sets and fits
f_ = clf;
figure(f_);
set(f_,'Units','Pixels','Position',[441 247 680 474]);
% Line handles and text for the legend.
legh_ = [];
legt_ = {};
% Limits of the x-axis.
xlim_ = [Inf -Inf];
% Axes for the plot.
ax_ = axes;
set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]);
set(ax_,'Box','on');
axes(ax_);
hold on;

% --- Plot data that was originally in data set "y vs. x"
x = x(:);
y = y(:);
h_ = line(x,y,'Parent',ax_,'Color',[0.333333 0 0.666667],...
    'LineStyle','none', 'LineWidth',1,...
    'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(x));
xlim_(2) = max(xlim_(2),max(x));
legh_(end+1) = h_;
legt_{end+1} = 'y vs. x';

% --- Plot data that was originally in data set "y1 vs. x1"
x1 = x1(:);
y1 = y1(:);
h_ = line(x1,y1,'Parent',ax_,'Color',[0.333333 0.666667 0],...
    'LineStyle','none', 'LineWidth',1,...
    'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(x1));
xlim_(2) = max(xlim_(2),max(x1));
legh_(end+1) = h_;
legt_{end+1} = 'y1 vs. x1';

% Nudge axis limits beyond data limits
if all(isfinite(xlim_))
    xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
    set(ax_,'XLim',xlim_)
else
    set(ax_, 'XLim',[9.3000000000000007, 80.700000000000003]);
end

% --- Create fit "fit 1"
ok_ = isfinite(x) & isfinite(y);
if ~all( ok_ )
    warning( 'GenerateMFile:IgnoringNansAndInfs',...
        'Ignoring NaNs and Infs in data.' );
end
ft_ = fittype('poly1');

% Fit this model using new data
cf_1 = fit(x(ok_),y(ok_),ft_);
% Alternatively uncomment the following lines to use coefficients from the
% original fit. You can use this choice to plot the original fit against new
% data.
%    cv_ = { 0.23728907152716422, 33.85252966088639};
%    cf_ = cfit(ft_,cv_{:});

% Plot this fit
h_ = plot(cf_1,'fit',0.95);
set(h_(1),'Color',[1 0 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);
% Turn off legend created by plot method.
legend off;
% Store line handle and fit name for legend.
legh_(end+1) = h_(1);
legt_{end+1} = 'fit 1';

% --- Create fit "fit 2"
ok_ = isfinite(x1) & isfinite(y1);
if ~all( ok_ )
    warning( 'GenerateMFile:IgnoringNansAndInfs',...
        'Ignoring NaNs and Infs in data.' );
end
ft_ = fittype('poly1');

% Fit this model using new data
cf_2= fit(x1(ok_),y1(ok_),ft_);
% Alternatively uncomment the following lines to use coefficients from the
% original fit. You can use this choice to plot the original fit against new
% data.
%    cv_ = { 0.13762715268932224, 50.828629244759306};
%    cf_ = cfit(ft_,cv_{:});

% Plot this fit
h_ = plot(cf_2,'fit',0.95);
set(h_(1),'Color',[0 0 1],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);
% Turn off legend created by plot method.
legend off;
% Store line handle and fit name for legend.
legh_(end+1) = h_(1);
legt_{end+1} = 'fit 2';

% --- Finished fitting and plotting data. Clean up.
hold off;
% Display legend
leginfo_ = {'Orientation', 'vertical', 'Location', 'NorthEast'};
h_ = legend(ax_,legh_,legt_,leginfo_{:});
set(h_,'Interpreter','none');
% Remove labels from x- and y-axes.
xlabel(ax_,'');
ylabel(ax_,'');

Contact us