Why does Matlab execute my code sometimes twice?

조회 수: 5 (최근 30일)
Bence
Bence 2013년 9월 24일
편집: John D'Errico 2021년 8월 7일
Sometimes when I press the run button in the Editor, the code is executed twice (or even more times). I can't exactly define when it happens, but it happened even to this very simple program code:
clear all
for z=1:5
z
pause(1)
end
Have you experienced similar problems? Any idea why it can happen?
  댓글 수: 8
Image Analyst
Image Analyst 2021년 8월 7일
@Alvin Stroyny, then same answer. Either you are calling it recursively or clicking the button twice. Maybe your keyboard is not being properly debounced - try a new one. Or try putting a breakpoint there.
John D'Errico
John D'Errico 2021년 8월 7일
편집: John D'Errico 2021년 8월 7일
Since this happens after a mouse click, I might check to see if you have some issue with your mouse. For example, swap a different mouse in. Or, see if it also repeats when using the run command directly, since you can run a script from the command line.

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

답변 (1개)

Image Analyst
Image Analyst 2013년 9월 24일
Most likely, you clicked on the green "Run" triangle twice, or you typed F5 twice. Either of those will cause it to run once, then immediately run again after it finished (I actually tried it).
  댓글 수: 2
Bence
Bence 2013년 9월 24일
Thanks for your answer. You are right that clicking the Run button twice makes the code run twice but since I recognised this problem I tried to click the Run button cautiously in order not to click it twice accidentally. And it doesn't explain why the same code once ran 7 times (not kidding).
Image Analyst
Image Analyst 2013년 9월 24일
Step through it one line at a time and see where it goes after you reach the end of the code.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by