how can I custom a mask matrix by giving black color to values equal 1 and transparency to NaN values?

9 views (last 30 days)
I found a mask matrix, and I need to apply black color to values equal 1 and transparency to NaN values. I need a cycle of commands in order to obtain (with the pcolor function) a black mask on a transparent background (NaN values) to be superimposed on a second matrix. Could someone help me please?

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 22 Nov 2021
Edited: Benjamin Kraus on 22 Nov 2021
I'm not 100% sure what you are asking, but if I understand your question correctly I don't think what you are asking can be done easily with pcolor alone.
I think you want to use imagesc instead. The problem is that you cannot set the color of individual faces in the surface object created by the pcolor command, you have to either set all the faces to the same color, or rely on the data, but if you set all the faces to the same color the AlphaData used to control transparency is ignored. You can hack around this by appending a color to the colormap, but it is easier to just use the image object, which doesn't have those restrictions.
You can use imagesc instead of pcolor for both the data and the mask, or use pcolor for the data and imagesc for the mask. The downside is that it will be harder to align the two if you do that.
matrix1 = peaks;
% Create a mask with either 1 or NaN. For the sake of an example, the
% "mask" will be all 1s except for two squares in the middle.
maskmatrix = ones(size(peaks));
maskmatrix(5:20,15:35) = NaN;
maskmatrix(30:45,15:35) = NaN;
% Draw the data using pcolor.
% Convert the mask into an [n m 3] image of all zeros (for black).
maskim = repmat(maskmatrix~=1, 1, 1, 3);
hold on
h = imagesc(maskim);
% Set the AlphaData to 0 where there is a NaN in the mask so the NaN values
% are transparent.
h.AlphaData = ~isnan(maskmatrix);
% (optional) Set AlphaData to 0.6 (instead of 1) everywhere else so you can see some of
% the data under the mask.
h.AlphaData = ~isnan(maskmatrix)*0.6;

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!