File Exchange

image thumbnail


version (1.09 KB) by Mikhail
Interrupt execution of script or function after specified period of time


Updated 24 Jan 2017

View License

Interrupt execution of script, function or MATLAB expression after specified period of time. Similar to timeout from GNU coreutils.
This function uses undocumented command window method to programatically send Ctrl+C after specified period of time.

There are other ways of sending Ctrl+C programmatically, including Java Robot or SIGINT on Linux, but they require changes to the script or function that we want to interrupt.

This submission serves as a PoC and demonstration. Adapt to your specific needs if necessary.

Usage examples

Run the 'longComputations' script for no more than 5 seconds:
>> timeout('longComputations',5)

Interrupt execution of infinite pause after 3 seconds:
>> timeout('pause(inf)',3)

Cite As

Mikhail (2021). timeout (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)


@Abhinav Can you not see "Usage examples" section in the description?

Abhinav Gaur

Good idea. How can we use it to interrupt a function if it takes more than a certain amount of time to execute ?


How cruel to let Matlab simulate a press of Ctrl-C! It would be useful to stop and delete the timer, when the function ran successfully. I'd prefer providing a function handle and a list of inputs and outputs. But as you said, this piece of code is thought for a personal adaption. Therefore it does exactly, what is advertised in a smart and cruel way. :-)

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!