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

66 visualizaciones (últimos 30 días)
Anna S
Anna S el 23 de Mayo de 2019
Comentada: Adam Danz el 23 de Mayo de 2019
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 comentarios
Jan
Jan el 23 de Mayo de 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 el 23 de Mayo de 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 :)

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 23 de Mayo de 2019
Editada: Adam Danz el 23 de Mayo de 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')
  2 comentarios
Anna S
Anna S el 23 de Mayo de 2019
Thanks a lot - that is exactly what I'm looking for!
Adam Danz
Adam Danz el 23 de Mayo de 2019
Glad I could help.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by