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: bramila_maskdata.m
    path of file: ./repos/bspm/thirdparty/bramila
    
    the code of the file until where you have to start completion: function vol = bramila_maskdata(cfg)
% INPUT
%   cfg.infile = location where the subject NII file (4D)
%   cfg.vol = input can be a 4D volume, if this exists, then infile is not loaded but used as ID (optional)
%   cfg.write = 0 (default 0, set to 1 if you want to store the volume)
%   cfg.mask = optional, mask to use (it can be a 3D vol or a NII file or a MAT file)
% OUTPUT

    
function vol = bramila_maskdata(cfg)
% INPUT
%   cfg.infile = location where the subject NII file (4D)
%   cfg.vol = input can be a 4D volume, if this exists, then infile is not loaded but used as ID (optional)
%   cfg.write = 0 (default 0, set to 1 if you want to store the volume)
%   cfg.mask = optional, mask to use (it can be a 3D vol or a NII file or a MAT file)
% OUTPUT
%   vol = a 4D volume masked

if(isfield(cfg,'vol'))
	data=cfg.vol;
	% add check that it's a 4D vol
elseif(isfield(cfg,'infile'))
	nii=load_nii(cfg.infile);
	data=nii.img;
end