Main Content

Hold の状態への応答

この例では、hold の状態をテストし、ユーザー定義プロット関数で適切に応答する方法を説明します。

プロット関数は通常、さまざまなデータに合わせるためにいろいろな座標軸のパラメーターを変化させます。関数 myPlot3D は、次のように動作します。

  • 入力データに応じて、2 次元ビューまたは 3 次元ビューを使用する。

  • MATLAB® プロット関数の動作に準拠させるため、現在の hold の状態に従う。

function myPlot3D(x,y,z)
   % Call newplot to get the axes handle
   cax = newplot;
   % Save current hold state
   hold_state = ishold;
   % Call plotting commands to
   % produce custom graph
   if nargin == 2
      line(x,y);
      % Change view only if hold is off
      if ~hold_state
         view(cax,2)
      end
   elseif nargin == 3
      line(x,y,z);
      % Change view only if hold is off
      if ~hold_state
         view(cax,3)
      end
   end
   grid on
end

たとえば、myPlot3D の最初の呼び出しでは、3 次元のグラフが作成されます。2 番目の myPlot3D の呼び出しでは、holdon のため、2 次元データが 3 次元ビューに追加されます。

[x,y,z] = peaks(20);
myPlot3D(x,y,z)
hold on
myPlot3D(x,y)