Clear Filters
Clear Filters

Extract the information of inside and outside of a contourf

13 views (last 30 days)
Hello everyone,
I have a single line of code, quite simple, thanks to which I get (visually) exactly what I need:
h=figure; [pippo1, pippo2]=contourf(AZ,EL,FF, [0, 0]);
where the input arguments have size 301x301. The plot I get is like this:
As you can see, the outside of the contour is not only the big cyan area, but also some small areas in the white (not filled) polygon. I need the coordinates of the outside polygon (the whole cyan region) but I couldn't extract them from either "h" or "pippo2". Please note, I DON'T NEED the coordinates of the single contours, because if I extract XDATA and YDATA they lose the information about "the inside" and the "outside". I need to extract, in some way, a single contour that represents the whole cyan area, and another one that represents the complementary (white) one.
Thanks a lot!

Answers (1)

Garmit Pant
Garmit Pant on 9 Aug 2024
Hi Roberto
To extract the regions based on the output of the “contourf” function, you can use the contour matrix output of the “contour” function.
“contour” outputs a contour matrix that defines the contour lines. It is of the following form:
You can extract the line data from the matrix and then use “inpolygon” function to create binary masks to extract the regions inside and out the contour lines. The following code snippet demonstrates how to extract regions inside the contour lines:
% Generate sample data
[X, Y, Z] = peaks;
% Generate contour data
M = contourf(X, Y, Z, [0, 0]);
% Extract regions from contour data
r1 = M(:, 2:M(2, 1) + 1);
r2 = M(:, M(2, 1) + 1 + 2:M(2, 1) + 1 + 2 + M(2, M(2, 1) + 1 + 1) - 1);
r3 = M(:, M(2, 1) + 1 + 2 + M(2, M(2, 1) + 1 + 1) + 1:end);
% Plot the regions
figure;
plot(r1(1, :), r1(2, :), 'o');
hold on;
plot(r2(1, :), r2(2, :), 'o');
plot(r3(1, :), r3(2, :), 'o');
xlim([-3 3]);
ylim([-3 3]);
hold off;
% Create masks for each region
in_mask1 = inpolygon(X, Y, r1(1, :), r1(2, :));
in_mask2 = inpolygon(X, Y, r2(1, :), r2(2, :));
in_mask3 = inpolygon(X, Y, r3(1, :), r3(2, :));
% Combine the masks
in_maskmain = in_mask1 | in_mask2 | in_mask3;
% Visualize the combined mask
figure;
surf(X, Y, int8(in_maskmain));
title('Combined Mask');
xlabel('X');
ylabel('Y');
zlabel('Mask');
% Initialize the masked Z matrix with zeros (or another value like NaN)
Z_masked = zeros(size(Z)); % or Z_masked = NaN(size(Z));
% Assign the heights from Z within the region to Z_masked
Z_masked(in_maskmain) = Z(in_maskmain);
% Visualize the masked Z data
figure;
M2 = contourf(X, Y, Z_masked);
title('Masked Z Data');
xlabel('X');
ylabel('Y');
For further understanding, kindly refer to the following MathWorks Documentation:
I hope you find the above explanation and suggestions useful!

Categories

Find more on Contour Plots in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!