Without answering your question, here are some efficiency hints:
imgstack = zeros(32,128,1,no_of_frames, 'uint16');
map = ndgrid(0:1/64:1,[0 0 0]);
Specifying a frame number to imread() in the way you have is supported only for .CUR and .GIF files. If you are using .GIF files (but not .CUR files) then you could probably gain a fair bit of efficiency by reading all of the frames in a single call, by specifying 1:nframes as the index. .CUR files only support reading one frame at a time. The only other file type that supports reading frames is TIFF files, for which you must use
imread(filename, 'Index', frame)
and a vector of frames is not permitted for TIFF.