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

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

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)

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

When I do this:
goodValues = v(~isnan(v));
minV=min(goodValues,[],1);
maxV=max(goodValues,[],1);
I get this error:
Error using evalin
Undefined function 'isnan' for input arguments of type 'cell'.
Then v is a cell array (unfortunately). Try to avoid cell arrays. See if this will work
v = v{:}; % Use braces instead of parentheses.
Your v is a cell array for some reason, instead of containing intensity data. I speculate that your v is a cell array of character vectors that are file names.
What are you assigning to v?
I am able to find the maximum and I actually want the minimum restricted to 0, so this is what I have now, but now I am getting an error saying that the "Index exceeds matrix dimensions." Thoughts?
Thanks,
% Make an image of the points, then apply a colormap
x = x(:); % Extract x value
y = y(:); % Extract y value
v=v(:);
%find max and min values
minX = min(x,[],1);
maxX = max(x,[],1);
minY = min(y,[],1);
maxY = max(y,[],1);
%dealing with NaN
v(isnan(v)) = 0; %set all NaN to zero
minV=0;
maxV=max(v,[],1);
% 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));
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?
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.

Categories

Asked:

on 25 Sep 2017

Commented:

on 26 Sep 2017

Community Treasure Hunt

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

Start Hunting!