Extract XData and YData from contourm hggroup handle

Hello,
I would greatly appreciate some assistance to extract the XData and YData arrays, from the hgroup type handle of the contourm function:
[SC,h]=contourm(LatGrid,LonGrid,Cgrid,V,'Fill','on');
I require the XData and YData arrays as inputs to the following polybool function:
[xtrim, ytrim] = polybool('&', XData, YData, STlong, STlat);
thanks,
Mark

 Accepted Answer

Do you want to extract the latitude and longitude values, or the projected X/Y coordinates?
If the former, the coordinates are in the SC matrix in your example. Its format is a bit obscure, so I use a slightly-modified version of this contourcs function to extract it.
Below is an example that I wrote a while ago that I think does the exact thing you're attempting (masking a contour plot to show within a polygon only).
If you really want x/y coordinates, then you'll need to add
for ii = 1:length(Cout)
[Cout(ii).X2, Cout(ii).Y2] = mfwdtran(Cout(ii).Y, Cout(ii).X);
end
after you extract the contours.
------------
The full example:
% Load coastlines (replace with your polygon)
latlim = [23 50];
lonlim = [-127 -65];
Usa = shaperead('landareas', 'usegeo', true, 'bounding', [lonlim' latlim']);
[latusa, lonusa] = maptrimp(Usa(1).Lat, Usa(1).Lon, latlim, lonlim);
% Create contours (replace with your gridded data)
n = 50;
xdata = linspace(lonlim(1), lonlim(2), n);
ydata = linspace(latlim(1), latlim(2), n);
zdata = peaks(n);
figure;
usamap('conus');
plotm(latusa, lonusa, 'k');
[C,h] = contourm(ydata, xdata, zdata);
% contourcs-stolen code
K = 0;
n0 = 1;
while n0<=size(C,2)
K = K + 1;
n0 = n0 + C(2,n0) + 1;
end
el = cell(K,1);
Cout = struct('Level',el,'Length',el,'X',el,'Y',el);
n0 = 1;
for k = 1:K
Cout(k).Level = C(1,n0);
idx = (n0+1):(n0+C(2,n0));
Cout(k).Length = C(2,n0);
Cout(k).X = C(1,idx);
Cout(k).Y = C(2,idx);
n0 = idx(end) + 1; % next starting index
end
% Trim to coastlines
[xc, yc] = deal(cell(length(Cout),1));
for ii = 1:length(Cout)
[xc{ii}, yc{ii}] = polybool('&', Cout(ii).X, Cout(ii).Y, lonusa, latusa);
end
% Plot
figure;
usamap('conus');
plotm(latusa, lonusa, 'k');
cellfun(@(x,y) plotm(y,x,'r'), xc, yc);

7 Comments

Excellent code Kelly and thank you so much for your assistance.
I had to slightly modify it to work with my unprojected data, but it worked. However, there is one problem. I need the contour layer to be filled. Is there away to retain the filled contour information in the polybool clipping process?
thanks you so much,
Mark
Hmm, may have to experiment on that one...
The following example uses a slightly different approach so we can preserve the color data in the contours of a contourm plot. It uses the x/ydata (projected) instead of the data in the C matrix.
% Load coastlines (replace with your polygon)
latlim = [23 50];
lonlim = [-127 -65];
Usa = shaperead('landareas', 'usegeo', true, ...
'bounding', [lonlim' latlim']);
[latusa, lonusa] = maptrimp(Usa(1).Lat, Usa(1).Lon, latlim, lonlim);
[latusa, lonusa] = interpm(latusa, lonusa, 0.5); % Fixing north border
% Create contours (replace with your gridded data)
n = 50;
xdata = linspace(lonlim(1), lonlim(2), n);
ydata = linspace(latlim(1), latlim(2), n);
zdata = peaks(n);
figure;
usamap('conus');
plotm(latusa, lonusa, 'k');
[C,h] = contourm(ydata, xdata, zdata);
hpatch = get(h, 'children');
[xusa, yusa] = mfwdtran(latusa, lonusa);
for ii = 1:length(hpatch)
xp = get(hpatch(ii), 'xdata');
yp = get(hpatch(ii), 'ydata');
zp = get(hpatch(ii), 'zdata');
[xp, yp] = poly2cw(xp, yp);
[xnew, ynew] = polybool('&', xp, yp, xusa, yusa);
set(hpatch(ii), 'xdata', xnew, ...
'ydata', ynew, ...
'zdata', ones(size(xnew))*zp(1));
end
Getting this to work with contourfm is trickier, since it would involve breaking some of the patches into multiple ones (any NaN-delimited patch that represents two separate polygons will result in color spilling out, because, well, patches in a map axis are just horribly inconvenient like that...). Do you need filled contours?
Great work Kelly. Although I am having trouble integrating this into my data.
Would you be able to look it over and provide some guidance?
If so, I can send you the data by email, since this information cannot be shown over the inet.
thanks a ton,
Mark
Yes, my goal is to visualize the surface as filled contours.
thanks,
No promises, but if you contact me by email I can see if I have time in the next few days (not going to put my email on this forum, but you can contact me via my File Exchange author page).
thanks for the consideration Kelly.
Hi Kelly. Better that I send it this way. thanks again for your consideration.

Sign in to comment.

More Answers (1)

This approach should do it (despite I don't know neither contourm nor polybool )
  • find the handles of the line-objects
  • use get( line_handle, 'Ydata' );
  • etc
Hint:
>> plot( magic(5) )
>> fh = gcf;
>> lh = findobj( fh, 'Type', 'Line' );
>> y_data = get( lh(1), 'Ydata' );
>> y_data = cat( 1, y_data, get( lh(2), 'Ydata' ) );
>> y_data = cat( 1, y_data, get( lh(3), 'Ydata' ) );
>> y_data
y_data =
15 16 22 3 9
8 14 20 21 2
1 7 13 19 25

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!