Main Content

유지 상태에 대한 대응

이 예제에서는 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차원 그래프를 생성합니다. myPlot3D에 대한 두 번째 호출은 holdon이기 때문에 2차원 데이터를 3차원 보기에 추가합니다.

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