How do I create multiple axis objects in a GUI using MATLAB 7.7 (R2008b)?

2 views (last 30 days)
I would like to plot data with two y-axes inside a GUI, by creating two different axis objects (I cannot use PLOTYY). The first axis object already exists in the GUI. I am able to create a second axis object in the GUI, however, when I try to set the 'Position' property of the second axis to match that of the first, the second axis is no longer visible.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Jan 2010
The reason that you are unable to see both axes is that the units for the position of the axes created by GUIDE are initialized differently than axes created within a function or at the MATLAB command prompt. An axis created by GUIDE has the 'Units' property set as 'character' by default while an axis created in a script or function has the 'Units' property set as 'normalized' by default. To resolve this issue, you need to set the 'Units' property of an axis created later to 'character', and then set the position. The following code demonstrates the idea discussed above. It has been adapted from the "Using Multiple X- and Y-Axes" demo and uses the 'testGUI' files attached below.
testGUI %create the GUI containing an axis
ax1 = findall(0, 'type', 'axes'); %assumes the axis inside the GUI is the only axis
set(get(ax1, 'Parent'), 'HandleVisibility', 'on'); %set handle visibility to on
axes(ax1); %make ax1 the current axis
x1 = [0:.1:40];
y1 = 4.*cos(x1)./(x1+2);
x2 = [1:.2:20];
y2 = x2.^2./x2.^3;
hl1 = line(x1,y1,'Color','r'); %plot a line in the current axis
set(ax1,'XColor','r','YColor','r'); %set the axis colors of ax1
ax2 = axes('Units', 'character'); %create a new axis and set units to be character
set(ax2, 'Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k'); %position the new axis on the earlier existing axis
hl2 = line(x2,y2,'Color','k','Parent',ax2); %plot a line on the new axis

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Tags

Products


Release

R2008b

Community Treasure Hunt

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

Start Hunting!