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: TensorsFromPrincipal3d.m
    path of file: ./repos/CutAndDisplace/3dCode/SimpleExternalFunctions
    
    the code of the file until where you have to start completion: function [ CartSxx,CartSyy,CartSzz,CartSxy,CartSxz,CartSyz]...
    =TensorsFromPrincipal3d( S1,S2,S3,DirS1 )
% TensorsFromPrincipal3d: Function that computes the Cartesian stress
%                   tensor from the magnitude and directions of defined
%                   principal stresses. Use to compute tensors for the BEM
%                   boundary conditions. This prints these to your console.
%
%                   The 3rd stress direction (most compressive is presumed
%                   to be vertical). 
%
%               
% usage #1: Prints results to cmd window
% TensorsFromPrincipal3d( S1,S2,S3,DirS1 )
%
% usage #2: Outputs these for futher use
% [ CartSxx,CartSyy,CartSzz,CartSxy,CartSxz,CartSyz]...
%    =TensorsFromPrincipal3d( S1,S2,S3,DirS1 )
%
% Arguments: (input)
%     S1,S2,S3       - The magnitudes of the principal stresses. 
%
%     DirS1       - The azimuth of the S1 component (degrees). 
%                  (angle away from N clockwise)
%
% Arguments: (output)
% CartSxx,CartSyy,CartSzz
% CartSxy,CartSxz,CartSyz- The magnitudes of the calculated Cartesian
%                          stress tensor.
%
% Example usage:
%
%  S1=0;
%  S2=-2;
%  S3=0;
%  DirS1=45;

    
function [ CartSxx,CartSyy,CartSzz,CartSxy,CartSxz,CartSyz]...
    =TensorsFromPrincipal3d( S1,S2,S3,DirS1 )
% TensorsFromPrincipal3d: Function that computes the Cartesian stress
%                   tensor from the magnitude and directions of defined
%                   principal stresses. Use to compute tensors for the BEM
%                   boundary conditions. This prints these to your console.
%
%                   The 3rd stress direction (most compressive is presumed
%                   to be vertical). 
%
%               
% usage #1: Prints results to cmd window
% TensorsFromPrincipal3d( S1,S2,S3,DirS1 )
%
% usage #2: Outputs these for futher use
% [ CartSxx,CartSyy,CartSzz,CartSxy,CartSxz,CartSyz]...
%    =TensorsFromPrincipal3d( S1,S2,S3,DirS1 )
%
% Arguments: (input)
%     S1,S2,S3       - The magnitudes of the principal stresses. 
%
%     DirS1       - The azimuth of the S1 component (degrees). 
%                  (angle away from N clockwise)
%
% Arguments: (output)
% CartSxx,CartSyy,CartSzz
% CartSxy,CartSxz,CartSyz- The magnitudes of the calculated Cartesian
%                          stress tensor.
%
% Example usage:
%
%  S1=0;
%  S2=-2;
%  S3=0;
%  DirS1=45;
%  TensorFromPrincipal2d( S1,S2,S3,DirS1 );
%
%
%  Author: Tim Davis
%  Copyright 2017, Tim Davis, Potsdam University\The University of Aberdeen


%% DefineMagnitude
%Horizontal stresses mags
Sz1= S1;    %Magnitude of component 1
Sz2= S2;	%Magnitude of component 2
%Vertical stress mag
Sz3= S3; 	%Magnitude of component 3

%% DefineDirection
Dir1=90-DirS1; 	%direction (az) of component 1 (degrees)
Dir2=90+Dir1; 	%direction (az) of component 2 (degrees)
%Dir 3 we presume is vertical so do not define this

%% Checking Dir 1&2 are 90 degrees apart
Test=round((asind(abs(sind(Dir1)))+asind(abs(sind(Dir2)))),8)==90; %rounding to 8 digits as precision can be an issue with this test
if Test==0
    error('Input Azimuth angles are not 90 degrees apart')
end