function mdplot(command)
% function to (update) the mooring plot
global U z H B Cd V W rho moorele X Y Z iobj
global h_edit_angle h_edit_elevation h_edit_plttitle

if isempty(U),
   U=[0 0 0 0];
   z=[sum(H(1,:))*1.2 sum(H(1,:)) sum(H(1,:))*0.1 0];
end
if nargin == 0, command=0; end
if ~isempty(H) & ~isempty(B) & ~isempty(Cd),
if command == 0,
   clear X Y Z iobj
   [X,Y Z,iobj]=moordyn;
   figure(3);clf;
   set(gcf,'Units', 'Normalized',...
      'Position',[.4 .3 .4 .5],...
      'Name','The Mooring',...
      'tag','mdplot');
   plot3(X,Y,Z,'b');hold on;
   li=length(iobj);
   plot3(X(iobj(2:li-1)),Y(iobj(2:li-1)),Z(iobj(2:li-1)),'or','Markersize',8);
   plot3(X(iobj(1)),Y(iobj(1)),Z(iobj(1)),'or','Markersize',10,'MarkerFaceColor','r');
   plot3(X(iobj(li)),Y(iobj(li)),Z(iobj(li)),'^b','Markersize',8,'MarkerFaceColor','b');
   grid;box on;axis equal;
   xlabel('X [m]');
   ylabel('Y [m]');
   zlabel('Z [m]');
   title('Mooring Design and Dynamics');
   drawnow;
   [az,el]=view;
   
   figure(4);clf
   set(gcf,'Units', 'Normalized',...
      'Position',[.05 .02 .3 .2],...
      'Name','Modify Plot',...
      'Color',[.8 .8 .8],...
      'tag','mdmodplt');
   tplot1=uicontrol('Style','text',...
      'String','3D View Angle: AZ',...
      'Units','normalized',...
      'Position',[.1 .8 .55 .125]);
   h_edit_angle=uicontrol('Style','edit',...
      'Callback','mdplot(1)',...
      'String',num2str(az),...
      'Units','Normalized',...
      'Position',[.7 .8 .2 .125]);
   tplot2=uicontrol('Style','text',...
      'String','3D View Elevation: EL',...
      'Units','normalized',...
      'Position',[.1 .6 .55 .125]);
   h_edit_elevation=uicontrol('Style','edit',...
      'Callback','mdplot(2)',...
      'String',num2str(el),...
      'Units','Normalized',...
      'Position',[.7 .6 .2 .125]);
   tplot3=uicontrol('Style','text',...
      'String','Plot Title',...
      'Units','normalized',...
      'Position',[.1 .4 .2 .125]);
   h_edit_plttitle=uicontrol('Style','edit',...
      'Callback','mdplot(3)',...
      'String','Mooring Design and Dynamics',...
      'Units','Normalized',...
      'Position',[.35 .4 .6 .125]);
   h_push_pltuvw=uicontrol('Style','Pushbutton',...
      'String','Plot Velocity Profiles',...
      'Units','normalized',...
      'Position',[.05 .2 .4 .125],...
      'Callback','mdplot(4)');
   h_push_rotate=uicontrol('Style','Pushbutton',...
      'String','Rotate3D',...
      'Units','normalized',...
      'Position',[.6 .2 .3 .125],...
      'Callback','mdplot(6)');
   h_push_print=uicontrol('Style','Pushbutton',...
      'String','Print',...
      'Units','normalized',...
      'Position',[.1 .02 .3 .125],...
      'Callback','mdplot(5)');
   h_push_cls=uicontrol('Style','Pushbutton',...
      'String','Close',...
      'Units','normalized',...
      'Position',[.6 .02 .3 .125],...
      'Callback','mdplot(7)');

elseif command == 1 | command == 2,
   figure(3);axis normal
   az=str2num(get(h_edit_angle,'String'));
   el=str2num(get(h_edit_elevation,'String'));
   view(az,el);
   axis equal
elseif command == 3,
   figure(3);axis normal
   title(get(h_edit_plttitle,'String'));
   axis equal
elseif command == 4,
   figure(3);axis normal
   xlim=get(gca,'XLim');
   ylim=get(gca,'YLim');
   zlim=get(gca,'ZLim');
   xmax1=max(abs(xlim));
   xmax2=xmax1*1.05;
   ymax1=max(abs(ylim));
   ymax2=ymax1*1.05;
   zmax1=max(abs(zlim));
   if max(abs(U))~=0,
      up=xmax1*U/(max(abs(U)));
   else
      up=[];
   end
   if max(abs(V)) ~=0, 
      vp=ymax1*V/(max(abs(V)));
   else
      vp=[];
   end
   if ~isempty(up),
      hup=plot3(up,ymax2*ones(size(up)),z,'g');
      set(hup,'Color',[0 0.5 0]);
   end
   if ~isempty(vp),
      hvp=plot3(xmax2*ones(size(vp)),vp,z,'m');
   end
   axis equal
   zlim=get(gca,'ZLim');
   zmax2=max(abs(zlim));
   set(gca,'XLim',[xlim(1) xmax2]);
   set(gca,'YLim',[ylim(1) ymax2]);
   if ~isempty(up),
      umax=max(abs(U));
      hut=text(xmax2*1.05,ymax2,zmax2,['|Umax|=',num2str(umax),' m/s']);
      set(hut,'Color',[0 0.5 0],'FontSize',8);
   end
   if ~isempty(vp),
      vmax=max(abs(V));
      hvt=text(xmax2*1.05,0,zmax2,['|Vmax|=',num2str(vmax),' m/s']);
      set(hvt,'Color','m','FontSize',8);
   end
elseif command == 5,
   figure(3);
   print
elseif command == 6,
   figure(3);
   rotate3d
elseif command ==7,
   close(3);
   close(4);
end
else
   disp(' Must load or enter a mooring before evaluation/plotting. ');
end

% fini
