Scheduling File execution using timers: Passing Outputs and/or alternatives

3 views (last 30 days)
Hello,
I am trying to figure out a way to execute code after a certain time without clogging up Matlab in the mean time (so no use of pause()). Best idea I had for doing so would be to create a timer, which will execute a script provided as string when the timer activates. This works so far, as long as I don't have to feed any more arguments either way. This is also why I can't schedule functions currently, because I can't figure out how I'd get matlab to distinguish between a string containing only a scriptname or a string containing an entire call including in-and output args.
In addition, I can't figure out how to retrieve outputs of said script/function being called through another function. I don't think that is possible to be extracted from a timer callback, and therefore I don't really know how to proceed.
I also realise that this could become problematic if I were to ever have more than one output, although I could probably bundle everything up into a structure and pass that out of the outermost function. The problem is getting outputs from the Callback into the schedFun-workspace so I can pass them out again.
I am not even sure if this is even possible.
%% // Main fun code
function [Out]=schedFun(FiletoTrigger,Time_in_Minutes,NumExec) % FiletoTrigger: string or char array. NumExec: Number of repetition
if nargin==2 % single execution
schedFun_Timer=timer('Name','Schedule Funtion Timer (ST)', ...
'Period',Time_in_Minutes*60, ...
'Tag','schedFunTimer', ...
'StartDelay',Time_in_Minutes*60, ...
'TasksToExecute',1, ...
'ExecutionMode','fixedSpacing', ...
'UserData',FiletoTrigger, ...
'TimerFcn',@myTimerCallbackSchedFun, ...
'StopFcn',@myTimerStopCallbackSchedfun);
elseif nargin==3 % multiple executions
schedFun_Timer=timer('Name','Schedule Funtion Timer (MT)', ...
'Period',Time_in_Minutes*60, ...
'Tag','schedFunTimer', ...
'StartDelay',Time_in_Minutes*60, ...
'TasksToExecute',NumExec, ...
'ExecutionMode','fixedSpacing', ...
'UserData',FiletoTrigger, ...
'TimerFcn',@myTimerCallbackSchedFun, ...
'StopFcn',@myTimerStopCallbackSchedfun);
end
start(schedFun_Timer)
end
function myTimerCallbackSchedFun(hObject,eventdata,Userdat)
% Out=eval(hObject.UserData) % I tried playing with both eval and evalc until I recognised the problem is
% Out=evalc(hObject.UserData) % passing results out of the callback. "run", as I am now aware, cannot assign
% run(hObject.UserData); % to outputs, so that's off the table. I am aware that eval is usually not recommended, but
disp('File has been run.') % I am not sure what else would be possible.
end
function myTimerStopCallbackSchedfun(hObject,eventdata,Userdat)
home
fprintf(('\nScheduler Timer:\n\nThe scheduler was shut down. \nThe file will not be run again.'));
end
Small unrelated rant about the question-editor here:
Why is it (still?) not possible to add a new line below a segment of code if it is at the very bottom of the editor? Whenever that happens, I have to copy everything out from the code section again, then undo until the code section itself disappears, enter another line or two, go up a few lines, insert a new code section and paste my code again. I know I will probably spent less time in my life doing that than I am writing this paragraph right now, but it irks me to no end.
Thank you & stay save.
C.A.

Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!