V = spm_vol(binaryMaskFile);% binaryMaskFile is one of the subject's files, e.g. their mask.niiĀ created after the first level stats
% now I change the name of the file
V.fname = 'test.nii';
% generate random data
data = randn(96,96,72);
% save it
spm_write_vol(V,data);
% read in the written volume and find the difference
diffVol = spm_read_vols(spm_vol('test.nii')) - data;
max(diffVol(:)) % I get 4.4715 in one example