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: spm_rfx_hist.m
    path of file: ./repos/CanlabCore/CanlabCore/diagnostics
    
    the code of the file until where you have to start completion: function spm_rfx_hist(cwd)
% :Usage:
% ::
%
%     function spm_rfx_hist(cwd)
%
% :Input:
%
%   a directory name where an spm or SnPM random effects analysis lives
%
% :Outputs:
%
%   histograms for all input images (usually contrast images from individual
%   subjects) plotted against a normal curve.
%
% The expected output is that each image will have roughly mean 0, with
% bumps or tails in the distribution of there are real activations in some
% parts of the brain.
%
% Looking at these histograms may be helpful for detecting outliers or 
% subjects with strange contrast values.  These may be caused by
% bad scaling, multicolinearity in the design matrix, acquisition artifacts,
% task-correlated head movement, or ???
%
% Histograms (blue) are overlaid on a Gaussian distribution (red)
% with a mean of 0 and a standard deviation equal to that of the observed data.
%
% ..
%    Tor Wager, 10/5/02
% ..

mypwd = pwd;
eval(['cd ' cwd])

fprintf(1,'\nLoading volumes.\t')

d = dir('SPMcfg.mat');

    
function spm_rfx_hist(cwd)
% :Usage:
% ::
%
%     function spm_rfx_hist(cwd)
%
% :Input:
%
%   a directory name where an spm or SnPM random effects analysis lives
%
% :Outputs:
%
%   histograms for all input images (usually contrast images from individual
%   subjects) plotted against a normal curve.
%
% The expected output is that each image will have roughly mean 0, with
% bumps or tails in the distribution of there are real activations in some
% parts of the brain.
%
% Looking at these histograms may be helpful for detecting outliers or 
% subjects with strange contrast values.  These may be caused by
% bad scaling, multicolinearity in the design matrix, acquisition artifacts,
% task-correlated head movement, or ???
%
% Histograms (blue) are overlaid on a Gaussian distribution (red)
% with a mean of 0 and a standard deviation equal to that of the observed data.
%
% ..
%    Tor Wager, 10/5/02
% ..

mypwd = pwd;
eval(['cd ' cwd])

fprintf(1,'\nLoading volumes.\t')

d = dir('SPMcfg.mat');
d2 = dir('SnPMcfg.mat');
if length(d) > 0
    % then we have an SPM directory
    
    load SPMcfg
    P = str2mat(VY.fname);
    v = spm_read_vols(VY);

    % ----------------------------------------------------------------------------------
    % * load mask, and mask all subjects' contrast values
    % so that we show only those voxels existing for all subjects.
    % ----------------------------------------------------------------------------------

    vm = spm_read_vols(spm_vol('mask.img'));
    vm(vm == 0) = NaN;
    for i = 1:size(v,4)
        v(:,:,:,i) = v(:,:,:,i) .* vm;
    end