File Exchange

image thumbnail

copyaxes

version 1.2.0.0 (3.23 KB) by Mar Callau-Zori
Copy an axes object into another axes object

7 Downloads

Updated 28 Dec 2011

View Version History

View License

COPYAXES - copy a handle object axes inpt

COPYAXES(SOURCE, DESTINATION) - copy axes from SOURCE to DESTINATION

COPYAXES(..., isInSubplot) - if the destination is in a subplot figure
(default false).

COPYAXES(..., isLegend) - if the axes is a legend (only if isInSubplot is
true) (default false)

EXAMPLE: Copy a axes with plot

plot([1:0.1:2*pi], sin([1:0.1:2*pi]));
title('sin function')
xlabel('x')
ylabel('sin(x)')
ax = gca;

figure;
ax_new = axes;
copyaxes(ax, ax_new)

EXAMPLE: Copy a axes with bar

bar(rand(10,5),'stacked');
title('bar stacket function')
xlabel('x label')
ylabel('y label')
ax = gca;

figure;
ax_new = axes;
copyaxes(ax, ax_new)

EXAMPLE: Copy a axes and legend (the legend is an axes object)

plot([1:0.1:2*pi], sin([1:0.1:2*pi]));
title('sin function')
xlabel('x')
ylabel('sin(x)')
lg = legend('sin');
ax = gca;

figure;
ax_new = axes;
lg_new = axes;
copyaxes(ax, ax_new)
copyaxes(lg, lg_new)

EXAMPLE: Copy an axes with a surface in a subplot.
Colormap is a figure property, not axes property.

k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
surf(x,y,z,c);
axis equal
title('sphere')
xlabel('x label')
ylabel('y label')
zlabel('z label')
ax = gca;
colormap([1 1 0; 0 1 1])

figure;
ax_new = subplot(2,2,1);
copyaxes(ax, ax_new, true)
colormap([1 1 0; 0 1 1])

EXAMPLE: Copy a surface in a subplot

[x,y] = meshgrid([-2:.4:2]);
Z = x.*exp(-x.^2-y.^2);
fh = figure('Position',[350 275 400 300],'Color','w');
ax = subplot(1,2,1);
set(ax, 'Color',[.8 .8 .8],'XTick',[-2 -1 0 1 2],...
'YTick',[-2 -1 0 1 2]);
sh = surface('XData',x,'YData',y,'ZData',Z,...
'FaceColor',get(ax,'Color')+.1,...
'EdgeColor','k','Marker','o',...
'MarkerFaceColor',[.5 1 .85]);
view(3)

ax_new = subplot(1,2,2);
copyaxes(ax, ax_new, true)

EXAMPLE: Copy an hist in a subplot

x = -4:0.1:4;
y = randn(500,1);
figure;
ax = subplot(1,2,1);
hist(y,x)

ax_new = subplot(1,2,2);
copyaxes(ax, ax_new, true)

EXAMPLE: Copy an pie in a subplot

x = [1 3 0.5 2.5 2];
explode = [0 1 0 0 0];
figure;
colormap jet
ax = subplot(1,2,1);
pie(x,explode)

ax_new = subplot(1,2,2);
copyaxes(ax, ax_new, true)

EXAMPLE: Copy an figure in a subplot

load earth
figure
ax = subplot(1,2,1);
image(X); colormap(map)

ax_new = subplot(1,2,2);
copyaxes(ax, ax_new, true)

Cite As

Mar Callau-Zori (2021). copyaxes (https://www.mathworks.com/matlabcentral/fileexchange/34314-copyaxes), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

Robert

I had to add one line to get this to work (running version R2014b). It was throwing an error while trying to set a read-only property (CurrentPoint), so after line 311, I added one more error check:

&& ~strcmpi(e.identifier, 'MATLAB:class:SetProhibited')

Phillip

Just what I was looking for, nice work

Matthew Phillips

Steven

Well done. I was working on plotting in subplot and I realized that I could have spent several weeks to get this by myself. Thanks to Mar Callau-Zori my project is now going so smoothly with this wonderful function.

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories

Community Treasure Hunt

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

Start Hunting!