필터 지우기
필터 지우기

Shutdown at specific time

조회 수: 4 (최근 30일)
Inna Pelloso
Inna Pelloso 2022년 12월 21일
댓글: Inna Pelloso 2022년 12월 21일
Hi,
What is the best way to make Matlab shutdown at a specific time?
I am running a timer object, tmr, and want to exit matlab when that has completed execution.
tmr = timer ...
( 'Name' , 'my_timer' ...
, 'TimerFcn' , @(~,~) my_script ...
, 'BusyMode' , 'drop' ...
, 'ExecutionMode' , 'fixedDelay' ...
, 'Period' , 10 ...
, 'TasksToExecute', 3 ...
, 'StartDelay' , 0 ...
);
%--------------------------------------------------------------------------
fTime = datetime([year(x) month(x) day(x) 12 00 00]); % Firing time
startat(tmr,fTime)
And a script, my_script.m:
%%my_script
datestr(now,31)
Thank you,
IP

채택된 답변

Bora Eryilmaz
Bora Eryilmaz 2022년 12월 21일
Call exit in StopFcn of the Timer object.
  댓글 수: 1
Inna Pelloso
Inna Pelloso 2022년 12월 21일
Thank you!
I just added:
'StopFcn' , 'exit'

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

추가 답변 (2개)

Geoff Hayes
Geoff Hayes 2022년 12월 21일
@Inna Pelloso - if you know when (i.e. seconds from now) that you want to shut down MATLAB, then you could do something like
function exitMatlabWhenTimerExpires
localTimer = timer('TimerFcn',@timerCallback,...
'StartDelay',30,'ExecutionMode','SingleShot');
start(localTimer);
function timerCallback(~,~)
fprintf('Exiting MATLAB...\n');
exit;
end
end
In the above, the timer callback is called 30 seconds (the StartDelay) after the timer is started.
  댓글 수: 1
Inna Pelloso
Inna Pelloso 2022년 12월 21일
Thank you. Calling StopFcn is much easier.

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


Adam Danz
Adam Danz 2022년 12월 21일
편집: Adam Danz 2022년 12월 21일
This solution displays a confirmation dialog that indicates to users that MATLAB will close unless they decide to cancel the auto-close. The clock in the message box counts down to 0 and if the user does nothing, MATLAB will close.
This uses quit force which ignores the finish.m file and it ignores the exit confirmation prompt if it is enabled. If you want don't want to ignore those, just use quit.
tmr = timer ...
( 'Name' , 'my_timer' ...
, 'TimerFcn' , @autoCloseMATLABfcn ...
, 'BusyMode' , 'drop' ...
, 'StartDelay' , 0 ...
);
fTime = datetime([year(x) month(x) day(x) 12 00 00]);
startat(tmr,fTime)
% For testing, replace the fTime line with
% fTime = datetime('now') + seconds(3); % prompt in 3 seconds
function autoCloseMATLABfcn(tobj,~)
delay = 30; % seconds
msgboxFcn = @(d)sprintf('Exiting MATLAB in %d seconds. Do you want to cancel auto-exit? ', d);
mb = msgbox(msgboxFcn(delay),'Exiting','warn');
starttime = tic;
while isvalid(mb) && toc(starttime)<delay
% keep waiting unless msgbox closes or time expires
timeleft = floor(delay - toc(starttime));
set(findobj(mb,'type','text','Tag','MessageBox'), 'String', msgboxFcn(timeleft)) % update text
drawnow % update the msgbox display
end
stop(tobj)
delete(tobj)
if isvalid(mb)
close(mb)
quit force % "force" ignores the finish.m file and exit confirmation
end
end
  댓글 수: 1
Inna Pelloso
Inna Pelloso 2022년 12월 21일
Thank you again. I'm firing Matlab siliently from a crontab (Linux)...so don't want any confirmation dialogs.

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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by