%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Plotting 1D line scans
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% General script for plotting 1D stray field line scans
% Starting with the measured ESR resonance, convert to stray field, then
% plot

% input: location of 'data.xlsx' (string)
%      : name of sheet (string)
% output: plot of the stray field line scan

% Modified by N. Hedrich 22.07.2020

%%
%%%%%%%%%% Initialization %%%%%%%%%%
clear;clc;

%%%%%%%%%% WHAT YOU NEED TO ENTER %%%%%%%%%%
%location of file to load.
file = 'data.xlsx';
%sheet to read out
sheet = 'Fig1D (1D)';

%%%%%%%%%% Load Data %%%%%%%%%%%%%%%%%%%
opts = detectImportOptions(file); 
opts.DataRange = 'A4'; %define start of data
opts.Sheet = sheet; %select the correct sheet

T = readtable(file,opts); %read the xlsx file

pos = T.Var2; %extract x values (um)
pos = pos(~isnan(pos)); %remove any possible NaN values

ESR = T.Var1; %extract y values (um)
ESR = ESR(~isnan(ESR)); %remove any possible NaN values

%%%calculate the background ESR resonance location
L1 = mean(ESR(1:10)); %take the average of the first 10 points
Lend = mean(ESR(:,end)); %take the average of the last 10 points
bkg = (L1+Lend)/2; %take the average of these values as the background 

field = 100*(bkg - ESR)/0.0028; %convert the ESR resonance value (GHz) to stray field (uT)

%%%%%%%%%% Plotting %%%%%%%%%%%%

figure;
plot(pos,field,'o-',...
       'MarkerEdgeColor','k',...
       'MarkerFaceColor',[102/255 102/255 102/255],...
       'MarkerSize',5,...
       'LineWidth',0.5,...
       'Color','k');
box on;
axis tight;

%%%axis labels
ylabel('Stray Field (uT)');
xlabel('Position (um)');

