Dear Hamed,

Indeed the problem lies in the header of the image that you were using beforehand. Images can be saved in different datatypes, most commonly: uint8 (for masks), int16 (once it was the most common) or float32 (even in the past it was the default for beta images). You can see the datatype used in V.dt (where V is V=spm_vol (image_file_name).).

Obviously, when you save data in integer formats you need to apply a proper scaling.  The scaling factor is saved in V.pinfo. If you're writing files in the float format this factor is irrelevant. If you're working with integers it must be set appropriately not to lose any information. Spm_write_volume automatically computes the correct V.pinfo, but only if the field is empty. Otherwise it applies the one it already has and, generally, clips everything. Therefore I'd reccomend doing V = rmfield (V,'pinfo') before spm_write_volume

Hope this helps!

Da: SPM (Statistical Parametric Mapping) <[log in to unmask]> per conto di hamed nili <[log in to unmask]>
Inviato: domenica 17 luglio 2016 19.48.06
A: [log in to unmask]
Oggetto: Re: [SPM] issue with spm_write_vol! Has anything changed?

I think I figures it out!
The header I was using was the header of one of the binary mask images.
I found (through experience) that if you want to write a volume with non-discrete values, you should use the header from say a file that takes non-discrete values (e.g. a beta file). Now the maximum difference is in the order of 1e-7...


On Sun, Jul 17, 2016 at 1:40 PM, hamed nili <[log in to unmask]<mailto:[log in to unmask]>> wrote:
Dear all,

I am using spm12 for some of my analysis. I create images in subject's native space and firstly want to write them as Nifti images. So I use spm_write_vol. It's strange that the nifti image that I write is not the same as the image that I feed in the function!

Can anyone point out the mistake I am making or the solution they have.
Here is the code:

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


% 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

Rispetta l’ambiente: non stampare questa mail se non è necessario.
Respect the environment: if it's not necessary, don't print this mail.


Devolvi il tuo 5 per mille all’Ospedale San Raffaele: perché al centro della Ricerca ci sei TU. CODICE FISCALE: 07636600962, nel riquadro RICERCA SANITARIA. Non c’è cura, senza ricerca. Non c’è ricerca, senza il tuo 5xmille. Scopri come su