Hi, I am trying to write the following matlab code:
tic
for a=A
if (a==8)
disp('I found it');
break;
end
end
toc
but the moment I write tic in the command window it starts counting time, how can I tell matlab to start counting time only when the whole code in written?

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 17일

1 개 추천

tic will restart the counter whenever you run it. So you don't need to worry if you ran tic several times before. Just run it after you have finished your code, and it will count from that point forward.

댓글 수: 5

Ahmad Agbaria
Ahmad Agbaria 2020년 3월 17일
but I need it to count how much time my code take to run, can you provide working code?
Ameer Hamza
Ameer Hamza 2020년 3월 17일
The code you posted in question is a working example of using tic and toc. Are you pasting your code in the command window? Paste your code in a script file and press the run button in the "Editor" tab to run the code.
Ahmad Agbaria
Ahmad Agbaria 2020년 3월 17일
I discovered that way but I am looking for another solution in which I don't need to write my code in seperate file and pasting it again in the command window.
I would prefer to type my code in the command window and to wait untill I finish writing the whole code
MATLAB was specifically designed to write the testing code in a script file, and the command line is only to run a maximum of 1-2 lines of code. You can still do it, by not pressing enter after each line of code. Just press shift + enter to move to the next line. When you reach the end of the code, you can press enter to run the code.
For example, type like this
tic % [press shift + enter]
x = rand(100); % [press shift + enter]
y = rand(100); % [press shift + enter]
z = x + y; % [press shift + enter]
toc % [press enter]
Ahmad Agbaria
Ahmad Agbaria 2020년 3월 17일
Thanks!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2020년 3월 17일

댓글:

2020년 3월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by