Matlab: use axes handles of another figure for a new figure
61 次查看(过去 30 天)
显示 更早的评论
Hey :)
I want to define the axes for one figure and then use it for several scripts. I always want to create the same sort of map, only for different variables.
I tried to copy the axeshandles, but the example from the documentation of copyobj (https://de.mathworks.com/help/matlab/ref/copyobj.html) does not work for me - can someone tell me what my mistake is?
frame = 5;
latlim = [min(min(lat))-frame max(max(lat))+frame];
lonlim = [min(min(lon))-frame max(max(lon))+frame];
fig1 = figure;
worldmap(latlim,lonlim)
load coastlines
geoshow(coastlat,coastlon)
p = pcolorm(lat,lon,uvelocity)
colorbar
title('first figure')
fig2 = figure;
ax2 = axes;
copyobj(p,ax2);
pcolorm(lat,lon,vvelocity)
title('second figure')
The error I get is: Invalid parent handle argument
I also have an alternative idea to do the copy part in my own, but this doesn't work either...
frame = 5;
latlim = [min(min(lat))-frame max(max(lat))+frame];
lonlim = [min(min(lon))-frame max(max(lon))+frame];
fig1 = figure;
worldmap(latlim,lonlim)
load coastlines
geoshow(coastlat,coastlon)
pcolorm(lat,lon,uvelocity)
colorbar
title('first figure')
axnames = struct2cell(set(gca));
axvalues = struct2cell(get(gca, axnames));
fig2 = figure;
set(gca,axnames,axvalues)
pcolorm(lat,lon,vvelocity)
title('second figure')
Here I get the following error:
Error using matlab.graphics.axis.Axes/get
Value must be a cell array of strings.
To avoid this I'm using the struct2cell function(???)!
I'm working with Version R2016b
Thanks for help! :)
2 个评论
Jan
2019-5-23
It is not clear to me, what "define the axes for one figure and then use it for several scripts" means. You can create serveal axes objects and copy the contents of one axes into another one. With copyobj you can duplicate the contents of an axes in a newly created axes also. But I do not understand what "using and axis for several scripts" mean.
"The error I get is: Invalid parent handle argument" - please post a copy of the complete message. Then te readers do not have to guess, which command causes the problem. The same for:
"Error using matlab.graphics.axis.Axes/get
Value must be a cell array of strings."
So please explain, what you want to achieve actually.
采纳的回答
Adam Danz
2019-5-23
编辑:Adam Danz
2019-5-23
If you're trying to create several figures with the same format but different data, you'd want to copy the entire figure, and then load in different data.
% Create fake data
load topo %built-in sample data
[lat lon] = meshgrat(topo,topolegend,[90 180]);
% Create template figure (no data yet)
fh = figure();
axesm miller
axis off; framem on; gridm on;
colorbar
% Copy figure
fh2 = copyobj(fh, 0);
% Add data to first plot
ax = findobj(fh, 'Type', 'Axes');
pcolorm(lat,lon,topo, 'Parent', ax)
title(ax, 'First figure')
% Add data to second plot
ax = findobj(fh2, 'Type', 'Axes');
pcolorm(lat,lon,topo, 'Parent', ax)
title(ax, 'Second figure')
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!