function bodeaxes(Editor,Preferences,HostFig)
%BODEAXES  Initializes Bode Diagram axes.

%   Author(s): P. Gahinet
%   Copyright 1986-2002 The MathWorks, Inc. 
%   $Revision: 1.25.4.1 $ $Date: 2004/07/31 23:10:05 $

% Magnitude axes
MagAxes = axes('Parent',HostFig, ...
   'Units','norm', ...
   'Box','on',...
   'Visible','off', ...
   'Xlim',[0.1,10], ...
   'XtickLabel',[], ...
   'Ylim',round(unitconv([-20,20],'dB',Preferences.MagnitudeUnits)),...
   'ButtonDownFcn',{@LocalButtonDown Editor});
Editor.setdefaults(Preferences,MagAxes);

% Phase axes
PhaseAxes = axes('Parent',HostFig, ...
   'Units','norm', ...
   'Box','on',...
   'Visible','off', ...
   'Xlim',[0.1 10], ...
   'Ylim',round(unitconv([-90 90],'deg',Preferences.PhaseUnits)),...
   'ButtonDownFcn',{@LocalButtonDown Editor});
Editor.setdefaults(Preferences,PhaseAxes);

% Create @axespair wrappers and corresponding listeners
% RE: Set YlimMode to manual for proper limit conversion when changing units before loading data
Geometry =  struct(...
   'HeightRatio',[.55 .45],...
   'HorizontalGap',0,...
   'LeftMargin',0,...
   'TopMargin',0,...
   'VerticalGap',14);
Editor.Axes = ctrluis.axespair([MagAxes;PhaseAxes],...
   'Geometry',Geometry,...
   'Grid',Preferences.Grid,...
   'XLabel','Frequency',...
   'YLabel',{'Magnitude';'Phase'},...
   'XlimSharing','all',...
   'XScale',Preferences.FrequencyScale, ...
   'XUnits',Preferences.FrequencyUnits,...
   'YScale',{Preferences.MagnitudeScale;'linear'},...
   'YUnits',{Preferences.MagnitudeUnits;Preferences.PhaseUnits},...
   'LimitFcn', {@updatelims Editor}, ...
   'LayoutManager','off',...
   'EventManager',Editor.EventManager);

% Nyquist lines
Zlevel = Editor.zlevel('backgroundline');
NyqLines(1,1) = line(NaN,NaN,NaN, ...
   'Parent', MagAxes, 'XlimInclude','off', 'YlimInclude','off',...
   'Color', 'k', 'Linestyle', '-', 'HitTest', 'off', ...
   'HelpTopicKey', 'sisonyquistline');
YData = infline(-Inf,Inf);
npts = length(YData);
NyqLines(2,1) = line(repmat(NaN,[1 npts]), YData, repmat(Zlevel,[1 npts]), ...
   'Parent', PhaseAxes, 'XlimInclude','off', 'YlimInclude','off',...
   'Color', 'k', 'Linestyle','-', 'HitTest', 'off', ...
   'HelpTopicKey', 'sisonyquistline');

% Build structure of HG handles
Editor.HG = struct('BodePlot',[],...
   'Compensator',struct('Magnitude',[],'Phase',[]),...
   'NyquistLine',NyqLines,...
   'System',struct('Magnitude',[],'Phase',[]));


%----------------------- Callbacks ----------------------------------

function LocalButtonDown(hSrc,event,Editor)
% Button down callbacks
Editor.mouseevent('bd',hSrc);
