How do I create a session that is limited by time

2 views (last 30 days)
I am working on a program in matlab that runs a session that keeps track of activity baased upon positive and negatvie cues over an action factor but I want to change that to one in which over a x value of time the session runs and when said time is over the session ends. Any assisstance on this problem would be greatly appreciated.

Answers (2)

Adam Danz
Adam Danz on 17 Jul 2019
Edited: Adam Danz on 17 Jul 2019
Use a timer object that ends the session after a set duration of time. Here's an overview.
Setting up the timer may seem intimidating but it's a powerful tool that was built for tasks that you're describing.
[addendum]
The suggestion above assumes that "session" refers to some GUI, script, or function that runs the program you described. For example, if the program runs in a loop you could iteratively check the value of some flag that determines whether the session is still active. The timer callback function could set the value of that flag to signal the end of the session.
  2 Comments
Walter Roberson
Walter Roberson on 17 Jul 2019
Alternatively if the code is checking for a flag such as this then it could just directly check elapsed time without needing a timer.
Either way note that actual termination would not be until the next time the flag was examined, rather than asap after the termination request is made.
Setting a flag is a good strategy when there is user action such as clicking STOP that can request termination.
TADA
TADA on 17 Jul 2019
I would stick to the timer+flag in this case rather than checking for elapsed time.
it would require less changes when you will be required to add the user activity flags back alongside session timeout.

Sign in to comment.


Walter Roberson
Walter Roberson on 17 Jul 2019
I have only found four ways in MATLAB to force a function to end without its cooperation:
  1. Java robot class to send control C to the command line. This is not foolproof because control c does not interrupt calls to the symbolic engine or to lapack (automatically parallel mathematical calculations) or to most DLLs.
  2. deliberately using more memory than exists. The out of memory error cannot be caught.
  3. use parallel computing toolbox to execute a batch or parfeval and cancel() the future when the time is up
  4. start a second matlab instance and kill the process when the time expires.

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!