%inputs:
%img: a full colour (3D), or greyscale image (2D)
%blocksize: a 2-element vector giving the height and width of each block
%
%precondition: the image size is a multiple of blocksize
assert(all(mod([size(img, 1); size(img, 2)], blocksize(:)) == 0), 'size of image is not a multiple of blocksize');
numblocks = [size(img, 1); size(img, 2)] ./ blocksize(:);
subimages = mat2cell(img, ones(1, numblocks(1)) * blocksize(1), ones(1, numblocks(2)) * blocksize(2), size(img, 3));
subimages is a cell array containing each 256x256 block. You can then save these in a loop. mat2cell is the function that does the extraction of each block.