I also tried to replace the 5th line code as follows:
referenceInfo = imref3d(size(medVol.Voxels),[-272.5 239.5],[-272.5 239.5],[-215 140])
the first coordinates of the first two pair -272.5 -272.5 and [-215 140] is "medVol38.VolumeGeometry.Position" and 239.5 is -272.5 + 512, where 512 is the length of the CT image.
It seems to work. But I don't know why the direction of "medVol.Voxels" is rotating 90 degrees to medVol original direction counterclockwise....
I don't know what to do. Can anyone give me some suggestions?