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: stackSlice.m
    path of file: ./repos/matImage/matImage/imStacks
    
    the code of the file until where you have to start completion: function res = stackSlice(img, dir, slice)
%STACKSLICE Extract a planar slice from a 3D image
%
%   SLICE = stackSlice(IMG, DIR, INDEX)
%   IMG is either a 3D or a 4D (3D+color) image.
%   DIR is 1, 2 or 3, or 'x', 'y', 'z'
%   INDEX is the slice index, between 1 and the number of voxels in the DIR
%   direction (using physical indexing).
%
%   If the input image has size NY-by-NX-by-NZ (gray scale image), or
%   NY-by-NX-by-3-by-NZ (color image), then the resulting slice has size: 
%   - NZ-by-NY(-by-3) if DIR is 1 or DIR is 'x'
%   - NX-by-NZ(-by-3) if DIR is 2 or DIR is 'y'
%   - NY-by-NX(-by-3) if DIR is 3 or DIR is 'z'
%   
%   The functions returns a planar image with the same type as the
%   original. 
%
%

    
function res = stackSlice(img, dir, slice)
%STACKSLICE Extract a planar slice from a 3D image
%
%   SLICE = stackSlice(IMG, DIR, INDEX)
%   IMG is either a 3D or a 4D (3D+color) image.
%   DIR is 1, 2 or 3, or 'x', 'y', 'z'
%   INDEX is the slice index, between 1 and the number of voxels in the DIR
%   direction (using physical indexing).
%
%   If the input image has size NY-by-NX-by-NZ (gray scale image), or
%   NY-by-NX-by-3-by-NZ (color image), then the resulting slice has size: 
%   - NZ-by-NY(-by-3) if DIR is 1 or DIR is 'x'
%   - NX-by-NZ(-by-3) if DIR is 2 or DIR is 'y'
%   - NY-by-NX(-by-3) if DIR is 3 or DIR is 'z'
%   
%   The functions returns a planar image with the same type as the
%   original. 
%
%
%   Example
%   % Display 3 slices of a MRI head
%     img = analyze75read(analyze75info('brainMRI.hdr'));
%     figure(1); clf; hold on;
%     for i=1:3
%         subplot(3, 1, i);
%         imshow(stackSlice(img, 'x', 20+20*i)');
%         set(gca, 'ydir', 'normal')
%     end