필터 지우기
필터 지우기

Matlab Tic and toc print something every .5 seconds?

조회 수: 29 (최근 30일)
Andrew
Andrew 2013년 9월 15일
편집: Ryan 2013년 11월 6일
Use a while loop to do the following for a total of 5 seconds. Every half second print, using
fprintf, the message “The elapsed time is:” followed by the time
This is all I've come up with after looking at it for over an hr..
clear, clc
close all
tic
timerVal = tic;
value = toc(timerVal);
while value <= 5
I cant seem to figure this out... i can easily get it to print the elasped time up to 5 seconds but i have no idea how to make it print every .5 seconds... Any help would be appreciated

답변 (3개)

Ryan
Ryan 2013년 11월 6일
편집: Ryan 2013년 11월 6일
tic
while toc<=10
pause(0.5)
fprintf('elapsed time is: %.2f seconds. \n',toc')
end

Walter Roberson
Walter Roberson 2013년 9월 16일
start a timer. Loop. Grab the elapsed time for the timer. continue in the loop if the elapsed time is less than 1/2 second. After the loop, stop the timer if need be.
  댓글 수: 2
Andrew
Andrew 2013년 9월 16일
So if I wanted to go all the way to 5 seconds I would essentially make 10 loops for each .5 seconds and display the time right after ea loop?
Walter Roberson
Walter Roberson 2013년 9월 16일
편집: Image Analyst 2013년 9월 16일
do the body of this loop until 5 seconds have elapsed
do the body of this loop until 1/2 second has elapsed
pause, or do some busy work to waste time
end of inner loop
my gosh, we're half a second later than we were before. Oh what shall we do?
end of outer loop

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


Image Analyst
Image Analyst 2013년 9월 16일
How about if you do a for loop where there is a pause(0.5) and a fprintf() inside the loop?
tic
for k = 1 : 10
pause(0.5);
fprintf('Elapsed time = %.2f seconds.\n', toc);
end
  댓글 수: 2
Andrew
Andrew 2013년 9월 16일
Is there a way to not use a pause function, like will I have to make multiple loops if I don't pause?
Image Analyst
Image Analyst 2013년 9월 16일
What's wrong with pause() - it seems to fit your homework question guidelines and doesn't seem to be excluded. Otherwise you can use a timer but it's trickier and more difficult. I'll give you one hint. Here's some code to stop all timers.
% Delete all timers from memory.
listOfTimers = timerfindall
if ~isempty(listOfTimers)
delete(listOfTimers(:));
end
You need to do something, so I'll let you figure out how to start the timer. The advantage of a timer is that you can do something in between timer events - you're not hung up like you are with pause().

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

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by