Extract XData and YData from contourm hggroup handle
11 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Kelly Kearney
on 23 Jan 2014
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
More Answers (1)
per isakson
on 23 Jan 2014
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
0 Comments
See Also
Categories
Find more on Startup and Shutdown 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!