필터 지우기
필터 지우기

How can I clear a graph before drawing a new one?

조회 수: 26 (최근 30일)
Jeroen
Jeroen 2013년 4월 22일
I wrote a function (let's call it 'test') that draws a graph (the formula is given as input) with the 'ezplot' function and some rectangles, who's position depends on the graph. The 'test' function is called different times by another function, so all the rectangles are drawn in the window by using
hold all
in 'test's code. When I call 'test' a first time, everything works fine.
However, when I don't close the graph window and call 'test' again with another formula, the new graph and rectangles are drawn in the same window as the first graph and everything overlaps eachother.
Can I draw the second graph in the same window, but clear that window (in order to delete the first graph and rectangles) first? Or is it even possible to draw it in a new window?

답변 (2개)

Matt Kindig
Matt Kindig 2013년 4월 22일
To clear the existing plot first:
cla(gca);
ezplot(...);
To draw a plot in a new window:
figure(); ezplot(....)
You should also learn about the 'hold' command, which allows you to control this clearing behavior.
doc hold
  댓글 수: 1
Jeroen
Jeroen 2013년 4월 22일
편집: Jeroen 2013년 4월 22일
When I put
cla(gca);
in front of my code, only the last rectangle and the graph are drawn. When I use
figure();
every rectangle is drawn in a different window, together with the graph. I think we're on the right way, but we're not exactly there yet.
I edited my question, to clarify that I already used 'hold all'.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2013년 4월 22일
Pass the figure number of the graph figure to clf()
  댓글 수: 2
Jeroen
Jeroen 2013년 4월 22일
When I use clf(), only the last rectangle and the graph are drawn, because 'test' is called different times. I updated my question to clarify my situation.
Walter Roberson
Walter Roberson 2013년 4월 22일
You could pass a flag indicating whether to do the clf() or not. Or you could use "persistent" to hold on to a copy of the formula, and when you detect that the current formula is different than the memorized one, do the clear (and update the memorized version.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Polygons에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by