A source of confusion is where the origin (the [0,0,0] location of an image) is stored. When there is no associated .mat file, the origin is read from the Analyze originator field. If this is zero it is assumed to match the center of the image field of view. If thereisa .mat file, then the origin is the first three values of
M\[0 0 0 1]'
whereMis the transformation matrix in the .mat file.
One limitation is that the origin stored in the Analyze header is a (short) integer, and so cannot represent an origin with fractional values. To set the origin to specific, fractional value, use this code snippet:
Orig = [ x y z ]; % Desired origin in units of voxels P = spm_get(Inf,'*.img'); % matrix of file names for i=1:size(P,1) M = spm_get_space(deblank(P(i,:))); R = M(1:3,1:3); % Set origin M(1:3,4) = -R*Orig(:); spm_get_space(deblank(P(i,:)),M); end