clear
close all


% read in the data
data = readtable("calibration_data.xlsx");

% pull out the mass (g)
mass_data = data{:,1}; % use curly braces to get the numerical values

% pull out the voltage (mV)
voltage_data = data{:,2}; % use curly braces to get the numerical values

% calculate the force (N), don't forget to convert from grams to kg!
force_data = mass_data .* 9.81 ./ 1000;

% make a scatter plot of the force vs. voltage
s = scatter(voltage_data,force_data);
s.SizeData = 70;
s.LineWidth = 3;
s.MarkerEdgeColor = "#0072BD"; % this is the pretty blue that MATLAB uses
s.Marker = "o";

% create a linear trend line
p = polyfit(voltage_data,force_data,1); % this gets the coefficients 
lineFit_xValues = [0 1000];
lineFit_yValues = polyval(p,lineFit_xValues); % this makes data points to plot.

% add the linear trend line to the plot
hold on;
linefit = plot(lineFit_xValues,lineFit_yValues);
linefit.LineWidth = 3; 
linefit.Color = "#0072BD";

% add the equation for the line fit to the plot
xCoordCaption = 50;
yCoordCaption = 9;
caption = sprintf('y = %f * x + %f', p(1), p(2));
text(xCoordCaption, yCoordCaption, caption, 'FontSize', 16, ...
    'Color', '#0072BD', 'FontWeight', 'bold','BackgroundColor','white');

% add arrow from equation to line
xa = [.4 .5]; % these are not coordinates, these are the positions across the whole graph
ya = [.68 .55]; % these are not coordinates, these are the positions across the whole graph
ar = annotation('arrow',xa,ya);
ar.Color = '#0072BD';
ar.LineWidth = 2;
ar.HeadWidth = 20;
ar.HeadLength = 20;

hold off;

% Improve the graph so it is easier to read and understand
grid on;
ax = gca;
ax.LineWidth = 2;
ax.FontSize = 16;
xlabel('voltage (mV)');
ylabel('force (N)');
ax.XLim = [0 800];
ax.YLim = [0 12];


% Save the plot to a .png file
print('sampleCalibrationGraph','-dpng');

