You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: cat_tst_BWPsliceartifact.m
    path of file: ./repos/ExploreASL/External/SPMmodified/toolbox/cat12
    
    the code of the file until where you have to start completion: %% create BWP slice artifact phantom
%  ---------------------------------------------------------------------
%  Use image without bias for better testing, as far as this correction
%  will also reduce the main inhomogeneity and not only the slice artifact.


% this function adds noise to the data to stabilize processing and we
% have to define a specific random pattern to get the same results each time
if exist('rng','file') == 2, rng('default'); rng(0); else, rand('state',0); randn('state',0); end


%BWP image
Pp0  = '/Volumes/MyBook/MRData/cat_tst/+RAW/BWP_Collins/SEG/p0BWPC_HC.nii';
P    = {
%  '/Volumes/4TBWD/MRData/database/BWP/results/BWPC_NIR/T1/BWPC_HC_T1_pn1_rf00000_vx100x100x100.nii'
  '/Volumes/4TBWD/MRData/database/BWP/results/BWPC_NIR/T1/BWPC_HC_T1_pn3_rf040pA_vx100x100x100.nii'
  '/Volumes/4TBWD/MRData/database/BWP/re
    
function adds noise to the data to stabilize processing and we
% have to define a specific random pattern to get the same results each time
if exist('rng','file') == 2, rng('default'); rng(0); else, rand('state',0); randn('state',0); end


%BWP image
Pp0  = '/Volumes/MyBook/MRData/cat_tst/+RAW/BWP_Collins/SEG/p0BWPC_HC.nii';
P    = {
%  '/Volumes/4TBWD/MRData/database/BWP/results/BWPC_NIR/T1/BWPC_HC_T1_pn1_rf00000_vx100x100x100.nii'
  '/Volumes/4TBWD/MRData/database/BWP/results/BWPC_NIR/T1/BWPC_HC_T1_pn3_rf040pA_vx100x100x100.nii'
  '/Volumes/4TBWD/MRData/database/BWP/results/BWPC_NIR/T1/BWPC_HC_T1_pn7_rf080pA_vx100x100x100.nii'
%  '/Volumes/4TBWD/MRData/database/BWP/results/BWPC_NIR/T1/BWPC_HC_T1_pn3_rf080pA_vx100x100x100.nii'
%  '/Volumes/4TBWD/MRData/database/BWP/results/BWPC_NIR/T1/BWPC_HC_T1_pn7_rf040pA_vx100x100x100.nii'
  };
rdir = '/Volumes/4TBWD/MRData/database/BWP/sliceart';
if ~exist(rdir,'dir'), mkdir(rdir); end

% parametersets
art.intraSlAmp  = 0.30:0.30:0.90; 
art.interSlAmp  = 0.20:0.20:0.60; 
art.intraSlFreq = [2 4 8];
art.interSlFreq = [0 2 8]; 

for pi=1:numel(P)
  % load image
  V = spm_vol(P{pi});
  Y = single(spm_read_vols(V));

  % write unbiased
  Vb = V; [pp,ff,ee] = fileparts(V.fname);
  Vb.fname = fullfile(rdir,sprintf('%s_sd0_amp%0.2f_freq%0.2f%s',ff,0,0,ee)); 
  spm_write_vol(Vb,Y);
  PGT = Vb.fname;

  %% parameter loop - Z
  PTT = cell(1,numel(art.intraSlAmp)*numel(art.interSlFreq)); iPPT = 1;
  for iae = 1:numel(art.intraSlAmp)
    iaa = 1; 
    for ief = 1:numel(art.interSlFreq)
      iaf = 1; %1:numel(art.interSlFreq)

      % add rand distortion
      [Yr,resTr] = cat_vol_resize(Y,'reduceV',1,45,1);
      Yas = 1 - art.intraSlAmp(iaa) + art.intraSlAmp(iaa) * ...
            randn([art.intraSlFreq(iaf) art.intraSlFreq(iaf) size(Y,3)],'single')/4; % RD20200115: replaced rand(size(YM))
      for z=1:size(Yas,3)
         Yas(:,:,z) = Yas(:,:,z) + (1-mean(mean(Yas(:,:,z))));
      end