[VoxBo] Reading voxbo files (cub tes res) in Matlab

Daniel Drucker ddrucker at psych.upenn.edu
Thu Nov 15 15:51:10 EST 2007


> Is there an easy way to read voxbo files (e.g. cub, tes, res, etc.) from
> matlab?

This is really, really inefficient, but this does the trick for me.
Note that you must have the SPM toolkit installed, and that you might
run into endian-ness issues. (If you fix those, please share your
solution back to me!)


function cub = readcub(cubfile)
% cub = readcub(filename)
% Extremely inefficiently reads a VoxBo cub as a 3D matrix.
% Inefficiently, because it uses SPM's spm_read_vols, which only deals with
% hdr/img format; thus, we actually shell out to VoxBo to convert the cub
% to that format, read, then delete the temp file we read.
%
% this function is smart enough to just read the img if passed a filename
% ending in img
%
% WARNING, this has endian-ness problems
%
% 2007 ddrucker at psych.upenn.edu

warning off; % quiet SPM's warning about flips
if strcmp('.img',cubfile(end-3:end))
    cub=spm_read_vols(spm_vol(cubfile));
else
    tmp = num2str(ceil(1E8*rand));
    system(['vb2img ' cubfile ' /tmp/' tmp '.img > /dev/null']);
    cub=spm_read_vols(spm_vol(['/tmp/' tmp '.img']));
    delete (['/tmp/' tmp '.img']);
    delete (['/tmp/' tmp '.hdr']);
end
warning on;




Here's something to write cubs, but I'm even less happy with this
code, and would greatly welcome comments from someone who understands
how the affine transformation matrix is supposed to go.

function writecub(filename,volume,datatype)
% WRITECUB(FILENAME,VOLUME) writes a functional cub.
% WARNING, this has endian-ness problems
%
% 2007 ddrucker at psych.upenn.edu

warning off; % ignoring the flipped warning
if nargin<3
    datatype=16384; % double swapped
end
if nargin==3 && strcmp(datatype,'mask')
    datatype=1024;
end

v.fname=[filename '.img'];
v.dim=[size(volume) datatype];
v.mat=eye(4,4); % not really sure if this is right
                % i might be supposed to say the 3x3x3 if it's a functional

spm_write_vol(v,volume);
system(['vb2cub ' filename '.hdr ' filename '.cub > /dev/null']);
delete([filename '.hdr']);
delete([filename '.img']);
warning on;



Anyway, this might help you get started.

Daniel Drucker


More information about the voxbo-general mailing list