Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

subplot problem

0 views
Skip to first unread message

Michael

unread,
Jul 20, 2008, 3:17:19 AM7/20/08
to
Hello,

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

Bruno Luong

unread,
Jul 20, 2008, 4:42:02 AM7/20/08
to
Michael <michael....@gmail.com> wrote in message
<6efb14a2-0ed8-40d5...@a2g2000prm.googlegroups.com>...

>
> 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


Rune Allnor

unread,
Jul 20, 2008, 12:43:19 PM7/20/08
to

Try to use either

fh2=subplot(2,1,1);
fh1=subplot(2,1,2);

or

fh2=gca;
fh1=gca;

in your code.

Rune

Matt Fig

unread,
Jul 21, 2008, 1:08:02 AM7/21/08
to
With only a slight alteration to your code, you could get it
to work.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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)

0 new messages