Clear Filters
Clear Filters

How to plot x,y, and intensity with NaN values present in the intensity data?

4 views (last 30 days)
Here is my code for my attempt to plot x, y, and intensity (referred to as v) on a color plot. I keep running into an issue where I cannot find the maximum of intensity due to NaN's present. Any advice to mitigate this issue or help refining this code is welcome. Thanks!
%plotting script
% Make an image of the points, then apply a colormap
x = x(:); % Extract x value
y = y(:); % Extract y value
v = v(:); % Extract intensity value
%find max and min values
minX = min(x,[],1);
maxX = max(x,[],1);
minY = min(y,[],1);
maxY = max(y,[],1);
[minV,~] = nanmin(v);
[maxV,~] = nanmax(v);
% Make image be 500 by 700
rows = 500;
columns = 700;
grayImage = zeros(rows, columns, 'uint8');
for k = 1 : length(x)
row = ceil((y(k) - minY) * rows / (maxY - minY));
column = ceil((x(k) - minX) * columns / (maxX - minX));
if (row == 0)
row = 1;
end
if (column == 0)
column = 1;
end
grayImage(row, column) = uint8(255 * (v(k) - minV) / (maxV - minV));
end
imshow(grayImage);
colorbar;
% Colormap is not gray scale.
% Apply some other colormap if you want
colormap(jet(256));
  1 Comment
Walter Roberson
Walter Roberson on 25 Sep 2017
You appear to me to be properly using nanmin() and nanmax().
The only thing I see is that you have been a little lax about handling v(k) being nan. Your calculation will result in uint8(nan) which turns out to be 0. If that is the result you want then I recommend adding a comment about that.
Also, the calculations (maxY - minY) and (maxV - minV) can be done ahead of time and stored in variables instead of being done every iteration. Also watch out for the possibility of constant input, in which case (maxV - minV) would be 0.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 25 Sep 2017
Edited: Image Analyst on 25 Sep 2017
I don't see any plotting going on. Anyway, to extract only non-NaN values from an array, such as grayImage or v or whatever, do this:
goodValues = v(~isnan(v));
Then you can get the max, min, mean, etc. of those good values.
  6 Comments
Image Analyst
Image Analyst on 25 Sep 2017
Edited: Image Analyst on 25 Sep 2017
We don't have your v so can't even run your code. So I quit at that point. Can you attach v in a .mat file, so we can try to help you?
Walter Roberson
Walter Roberson on 26 Sep 2017
In my test, I do not get index out of range.
Note: to get your color right, you should do
cmap = jet(256);
imshow(grayImage, cmap);
colorbar();
without calling colormap() after that.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!