Cell Arrays & Structures¶
Sometimes it is useful to be able to organise a lot of different things into some form of data structure. In SPM, you will mostly encounter these when you work with the batching system.
Cell Arrays¶
Cell arrays are collections of data, which can be of different data types or have different dimensions. Here’s a simple example for creating a cell array:
Values from the array can be read as:An empty cell array can be created with the cell
command, and it’s values filled in afterwards:
C = cell(2,3);
C{1,1} = eye(3);
C{2,1} = 3.1416;
C(:,2:3) = { 'some text', 'some more text'; uint8(32), {1,2,3}}
Structures¶
The struct
function is for creating an object with named fields, where each field has some sort of value.
This example shows the creation of a single struct
An array of structures can be created using cell arrays:
filenames = {'image1.nii','image2.nii','image3.nii'};
scales = {0.004, 0.003, 0.0035};
s = struct('fname',filenames, 'dims', [256 256 100], 'scalefactor', scales)
s2 = s(2)
We can change the contents of fields using the .
notation:
Later on, you will encounter structures of structures and cell arrays, within the SPM batching system. For example, this snippet shows batch job for spatially smoothing three T1w scans:
matlabbatch{1}.spm.spatial.smooth.data = {
'C:\Users\jashb\Data\IXI\IXI002-Guys-0828-T1.nii,1'
'C:\Users\jashb\Data\IXI\IXI012-HH-1211-T1.nii,1'
'C:\Users\jashb\Data\IXI\IXI013-HH-1212-T1.nii,1'
};
matlabbatch{1}.spm.spatial.smooth.fwhm = [8 8 8];
matlabbatch{1}.spm.spatial.smooth.dtype = 0;
matlabbatch{1}.spm.spatial.smooth.im = 0;
matlabbatch{1}.spm.spatial.smooth.prefix = 's';
disp(matlabbatch{1}.spm.spatial.smooth)
Objects¶
MATLAB can also be used for object-oriented programming, where objects can have different things done to them etc. This tutorial won’t say much about these, other than that it is possible to see what type of object you are dealing with:
It is also sometimes useful to see what variables have been defined on the worskpace, and what they contain. You can do this by typing whos
.
Sometimes you might also wish to remove variables from the workspace, as in the following example: