- Refer to the ‘Output Arguments’ section to understand the outputs generated: https://www.mathworks.com/help/releases/R2023b/matlab/ref/contourf.html
- Refer to the ‘Examples’ section: https://in.mathworks.com/help/releases/R2023b/matlab/ref/inpolygon.html
Extract the information of inside and outside of a contourf
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Roberto Tumolo
el 26 de Jul. de 2024
Respondida: Garmit Pant
el 9 de Ag. de 2024 a las 14:10
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1741431/image.png)
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!
0 comentarios
Respuestas (1)
Garmit Pant
el 9 de Ag. de 2024 a las 14:10
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!
0 comentarios
Ver también
Categorías
Más información sobre Cartesian Coordinate System Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!