run a for loop for 240 minutes

Answers (1)

% Runs for 5 seconds (i.e., 5/60 minutes)
T = 5/60; % minutes
ti = datetime('now');
tf = datetime('now');
while (minutes(tf-ti) < T)
% Do stuff
% Update current time
tf = datetime('now');
end
seconds(tf-ti) % Should be the same as T (in seconds), or
ans = 5.0000
minutes(tf-ti) % Should be the same as T (in minutes)
ans = 0.0833
But you can't rely on this for precise timing.

2 Comments

execusme sir, do you have any idea how i can computer the elapsed time in each iteration in for loop ?
You can use the tic/toc commands within the while loop:
tic
% Do stuff
elapsedTime = toc
elapsedTime = 0.0036

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 14 Dec 2022

Commented:

on 19 Dec 2022

Community Treasure Hunt

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

Start Hunting!