How can I keep figure boxes from popping up while running script.

I am very new to MATLAB and programming in general, so please bear with my syntax/poor code.
I am trying to make an animation for my project in an introductory MATLAB class. I want to show how different sorts "look" as they are being processed. I have the script doing what I want, but I can't get it to stop opening new figure windows for each loop pass. This results in hundreds of windows being opened.
Here is the bubble sort for example.
Ba=input('Array to be sorted: ')
n=length(Ba)
c=[1:length(Ba)]
counter1=1
for y=1:n-1
for x=1:n-1
if Ba(x+1)<Ba(x)
temp=Ba(x);
Ba(x)=Ba(x+1);
Ba(x+1)=temp;
counter1=counter1+1
figure('Visible','off');
bar(c,Ba);
saveas(gcf, sprintf('name%d', counter1), 'bmp');
end
end
end
It saves the images to be made into an animation later, but it is very slow as it opens hundreds of windows.

 채택된 답변

Evan
Evan 2013년 8월 1일
편집: Evan 2013년 8월 1일
Are you wanting to save the plot created on each pass of your loop? If so, just move the figure creation line out of the loop. As long as you're not holding your figure, the data plotted on its axes will be overwritten on each pass and then saved, without opening a new figure.
As an example, compare:
for i = 1:10
y = rand(1,30);
figure
plot(y);
saveas(gcf, sprintf('myfig_%d', i), 'bmp');
end
to:
figure
for i = 1:10
y = rand(1,30);
plot(y);
saveas(gcf, sprintf('myfig_%d', i), 'bmp');
end
So, in short, the below line needs to be moved outside your loop:
figure('Visible','off');

댓글 수: 4

Nick
Nick 2013년 8월 1일
편집: Nick 2013년 8월 1일
Thank you for your answer!
I moved the call to figure out of the loop like you said. Since it saves each figure, it doesn't matter if it is erased in MATLAB.
No problem! And be sure to accept an answer when your question has been fully answered and your problem resolved.
Has anyone else had a problem with this line?...
figure('Visible','off');
I tried this to stop all of my figures from popping up as they were generated, and it worked. However, afterwards, when I wanted to look at them, I couldn't open the figures. Has anyone else had a similar issue? How did you solve it?
"How did you solve it?"
Make them visible again.

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

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2013년 8월 1일

0 개 추천

Instead of making a new figure window on each loop iteration, use clf to clear the current one and reuse it!

댓글 수: 1

Thank you for your answer!
I ended up just moving the call to figure out of the loop. Since it saves each figure, it doesn't matter if it is erased in MATLAB.

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

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

질문:

2013년 8월 1일

댓글:

2018년 8월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by