MATLAB: Are logical arrays handled differently with the IMAGE function in MATLAB 6.5 (R13) than in previous versions

3darrayimagelogicalsMATLABr13typecasting

There are inconsistencies in the way logicals are treated in MATLAB 6.5 (R13). For example, if I have a 2D logical array, I would expect the image function to create a black and white image, as it did in previous versions of MATLAB:
testmat=(0.5<(rand(100,100)));
image(testmat);
Instead, the default HSV colormap is applied to the figure and the image colors are all in the blue color range.
If I use a 3D logical array, in previous versions I would get a black and white image. Now, I get an image that is all black. Setting the CData and CDataMapping properties has no effect on the image.
testmat=(0.5<(rand(100,100)));
mapmat = cat(3, testmat, testmat, testmat);
image(mapmat);

Best Answer

This bug has been fixed in Release 14 Service Pack 3 (R14SP3). For previous product releases, read below for any possible workarounds:
This problem occurs because of the change in data types for logical arrays in MATLAB 6.5 (R13). Previously, the IMAGE function treated these arrays as doubles.
To work around the problem, try the following code:
% For 2D arrays, explicitly set the colormap of the figure:
testmat=(0.5<(rand(100,100)));
image(testmat);
colormap([0 0 0; 1 1 1]);
% For 3D arrays, you can convert the data to uint8:
mapmat = uint8(cat(3, testmat, testmat, testmat));
image(mapmat);