function [p,S,mu] = nanpolyfit(x,y,n)

% This function runs polyfit, but excludes NaN values if they exist
if ~isequal(size(x), size(y));
    error('Sizes of x & y do not match')
end

x_vec = reshape(x,[],1);
y_vec = reshape(y,[],1);
xy_combo = [x_vec, y_vec];

xy_combo = xy_combo(~any(isnan(xy_combo),2),:); %removes any nan values

[~,~,mu] = polyfit(xy_combo(:,1),xy_combo(:,2),n); %for some reason, with "mu" can give erroroneous values for p & S
[p,S] = polyfit(xy_combo(:,1),xy_combo(:,2),n);

