File Exchange

image thumbnail

timer

version 1.0.0.0 (43.3 KB) by Igor Kaufman
TIMER.DLL is a MEX-file intended to create MATLAB callbacks caused by W95 timer events

1 Download

Updated 02 May 2006

View License

TIMER.DLL is a MEX-file intended to create MATLAB callbacks caused by W95 timer events for asynchronous work, e.g. for real-time data acquisition using MATLAB. It allows MATLAB to connect Windows timer with arbitrary MATLAB evaluation string (similar to conventional MATLAB callbacks). As defined these callbacks will be executed asynchronously every time when appropriate timer event occurs.

TMCLOCK.M presents the simplest example for using timer.dll namely real-time digital clock.

PLATFORM : Windows 95/NT/2000/XP.
MATLAB VERSION : 5.1.
COPYRIGHT : School of Physics and Chemistry, Lancaster
University, UK
AUTHOR : Dr Igor Kaufman
E-MAIL ADDRESS : I.KAUFMAN@LANCASTER.AC.UK

Cite As

Igor Kaufman (2021). timer (https://www.mathworks.com/matlabcentral/fileexchange/349-timer), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

Ben Stanley

Gergely, The reason that you had a problem is that the Matlab engine is not threadsafe, and cannot be called from another thread. This timer implementation gets around that problem in a very tricky way.

The timer implementation uses a Win32 TimerProc to call mexEvalString(). Matlab requires the engine to be called from within the same thread as Matlab is running (the engine is not threadsafe). It just so happens that the TimerProc is dispatched from Matlab's event loop (see MSDN documentation), which is the same thread as Matlab is running in, and so the single thread requirement is satisfied.

I am looking for a way to insert events into Matlab's event queue. The outline of a method for Windows would be:
1) from any thread, create a timer with a very small (or zero) timeout
2) the timer callback will then execute within Matlab's thread, when Matlab's thread is next idle.

Gergely Katona

I tried to implement this method to trigger matlab calls from a separate thread in my mex file. But Im doing something wrong (2007b). It somehow losts 'connection' and messages are not received any more. Could you pin the important steps in your source?

Paul Casey

Until Matlab introduced there own java version of a timer this has been the only timer avaialable fro matlab and of course if you compile your code then this is still the only choice. It underpins my companies whole Matlab application with timed calls to databases. Simply brilliant - never fails - 5 isn't a hig enough rating.

MATLAB Release Compatibility
Created with R10
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!