Matlab: use axes handles of another figure for a new figure

66 visualizzazioni (ultimi 30 giorni)
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 Commenti
Jan
Jan il 23 Mag 2019
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.
Anna S
Anna S il 23 Mag 2019
Sorry for being unclear, these are the complete error messages that I got for this code...
I meant that I just wanted to use the same 'frame'/properties of the axes for all the following figures. With 'different scripts' I tried to express, that I want to write some kind of function that I can use not only for this code, but also for other .m files just by calling :)
But the question is solved now, thanks anyway :)

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 23 Mag 2019
Modificato: Adam Danz il 23 Mag 2019
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')

Più risposte (0)

Prodotti


Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by