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

54 ビュー (過去 30 日間)
Anna S
Anna S 2019 年 5 月 23 日
コメント済み: Adam Danz 2019 年 5 月 23 日
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
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.
Anna S
Anna S 2019 年 5 月 23 日
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 :)

サインインしてコメントする。

採用された回答

Adam Danz
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')
  2 件のコメント
Anna S
Anna S 2019 年 5 月 23 日
Thanks a lot - that is exactly what I'm looking for!
Adam Danz
Adam Danz 2019 年 5 月 23 日
Glad I could help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by