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 个评论

but I need it to count how much time my code take to run, can you provide working code?
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.
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]

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by