function [n,vx,p] = Vregression(axis,varargin) %Varargin is in the form data1,pwr1,a1,b1,data2,pwr2,a1,b1,data3,pwr3,a1,b1,.... %This function will create a regression line for the linear region in the %vx vs sample number plot in the analysis of a Stuck Bead. %data1,.... is the file gained from the instrument %a1,b1,..... represent the initial and final value for the linear region %for each dataset %pwr1,.... is the power at which the data is gathered if strcmp(axis,'y') %uses the y component data for i=1:4:length(varargin); %plot vy vs sample number power=cell2mat(varargin(i+1)); data=cell2mat(varargin(i)); n=data(:,1); vy=data(:,3); figure(i); plot(n,vy); xlabel 'Time (ms)'; ylabel 'Vy'; title (['Vy vs. Time at ' num2str(power) 'mW']); grid; hold on; %Regression line a=cell2mat(varargin(i+2)); b=cell2mat(varargin(i+3)); n_1=data(a:b,1); vy_1=data(a:b,3); p=polyfit(n_1,vy_1,1); f=polyval(p,n_1); p_1=p(1); p_2=p(2); plot(n_1,f,'r'); legend('data',['equation fit (time = ' num2str(a) ' to ' num2str(b) '): ' num2str(p_1) 'x + ' num2str(p_2)]); hold off; end; elseif strcmp(axis,'x') %uses x component data for i=1:4:length(varargin); %plot vx vs sample number power=cell2mat(varargin(i+1)); data=cell2mat(varargin(i)); n=data(:,1); vx=data(:,2); figure(i); plot(n,vx); xlabel 'Time (ms)'; ylabel 'Vx'; title (['Vx vs. Time at ' num2str(power) 'mW']); grid; hold on; %Regression line a=cell2mat(varargin(i+2)); b=cell2mat(varargin(i+3)); n_1=data(a:b,1); vx_1=data(a:b,2); p=polyfit(n_1,vx_1,1); f=polyval(p,n_1); p_1=p(1); p_2=p(2); plot(n_1,f,'r'); legend('data',['equation fit (time = ' num2str(a) ' to ' num2str(b) '): ' num2str(p_1) 'x + ' num2str(p_2)]); hold off; end; else disp('wrong input format') end end