MATLAB's contour in the precense of Inf or NaN: splits one contour into two

5 views (last 30 days)
I'm using MATLAB's contourc function to extract X,Y coordinates of a field F along which it attains a single, constant value, as below:
cc = contourc(Xvec,Yvec,F,[Fval Fval]);
I then use contourdata to extract the X,Y data points from cc. We should get Fval if we linearly interpolate F onto any of these X,Y data points. However, as can be clearly seen in the figure (coloured cells show the value of F, while red and magenta lines show two different contours), this has failed to happen. What should be one contour has been split into two.
The dark blue in the lower left is -Inf data.
Before I dig too deep into the reproducibility of this, has anyone seen anything like this?
UPDATE: I've realized this does make some sense. The value of F where the two contours are closest is < Fval, and F in the lower left is -Inf also < Fval. Together they are a 'valley' that an Fval contour must stay above. The Fval contour becomes 'trapped', and the result is some strange behaviour wherein it tracks cell centres with finite values adjacent to -Inf cells.
My solution is to just change the -Inf to +Inf. Of course this is part of a larger dataset, so determining whether to adjust data to +Inf or -Inf is non-trivial. For that, I'm using imfill. My F increases along the 2nd dimension, so I'm using something like this:
Ffill = F;
Ffill(isnan(Ffill)) = -Inf;
Ffill = cat(2, -Inf(size(Ffill,1),1), Ffill, Inf(size(Ffill,1),1));
Ffill = imfill(Ffill);
Ffill = Ffill(:,2:end-1);
and then use contourc on Ffill.

Answers (1)

Star Strider
Star Strider on 20 Feb 2015
I am not certain what you’re doing, but the (X,Y) data provided by the first argument (that you are not using, or the ContourMatrix property), has as its first column the level of the contour and the number of points in the contour. If you use that column, you will get an anomalous result for the first value. See the ContourMatrix property of Contour Properties for details.
  2 Comments
Geoff Stanley
Geoff Stanley on 21 Feb 2015
Thanks. Indeed, using [~,hc] = contour(...) and then using get(hc, ...) as I was doing is indeed not the clearest way. After finding contourdata on FEX I'm now using contourc. However, that didn't affect the problem.
Star Strider
Star Strider on 21 Feb 2015
My pleasure.
The only suggestions I have is to see why your function (or data) are generating NaN values. If you are encountering ‘0/0’ problems, and if you have the Symbolic Math Toolbox, consider using L’Hospital’s Rule to circumvent them. If the NaN values are in your data, consider one of the interpolation functions to interpolate over them. No guarantees that even those will solve your problem, but they’re worth a go.
You seem to be proficient enough with MATLAB and ‘handle-diving’ that without knowing more of your data and code, it is unlikely that we here could be of any additional assistance.

Sign in to comment.

Categories

Find more on Contour Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!