Suppose an m-file "xSquare" which plots the square
and logarithm of an input vector. Note that there are two
axes in the the figure window. :
>> x=[1 2 3]
===========================
function xSquare(x)
fh2=axes;
pos=get(fh2,'Position');
height=0.4;
set(fh2,'Position',...
[pos(1) pos(2) pos(3) height])
fh1=axes;
set(fh1,'Position',...
[pos(1) (pos(2)+height) pos(3) height])
y=x.^2;
plot(fh1,x,y)
hold on
yy=log(x);
plot(fh2,x,yy,'g')
============================
I now want to use xSquare(x) in a new function
which uses subplot.
>> subplot(2,1,1)
>> xSquare(x)
The problem is that xSquare(x) doesn't plot into
the axes made by subplot.
My question is what needs to be done so
that the output of an m-file which contains more
than one axes can be used with the subplot function?
Thanks you,
-Michael
>
> My question is what needs to be done so
> that the output of an m-file which contains more
> than one axes can be used with the subplot function?
>
Michael, you should aware that a subplot command does do any
particular than creating an AXE and make it active.
So the subplot command is essentially incompatible with the
fact that your function creates two AXES in its own.
AXES are graphic objects, and all are direct Children of the
figure. There is no such thing as tree hierarchy of AXES.
Bruno
Try to use either
fh2=subplot(2,1,1);
fh1=subplot(2,1,2);
or
fh2=gca;
fh1=gca;
in your code.
Rune
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function xSquare(x,fh2)
if nargin==1
fh2=axes;
end
fh1 = axes;
pos = get(fh2,'Position');
height = pos(4)/2;
set(fh2,'Position',[pos(1) pos(2) pos(3) height])
set(fh1,'Position',[pos(1) (pos(2)+height) pos(3) height])
y = x.^2;
plot(fh1,x,y)
hold on
yy = log(x);
plot(fh2,x,yy,'g')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Now you can call it like this:
xSquare([1:.1:30])
or use it in subplots like you wanted. Then you would use
it like this:
ax1 = subplot(2,2,1);
ax2 = subplot(2,2,2);
ax3 = subplot(2,2,3);
ax4 = subplot(2,2,4);
xSquare([1:.1:30],ax2)
xSquare([-30:.1:-1],ax1)
xSquare([-1:.1:1],ax3)
xSquare([-10:.1:10],ax4)