Getting data from asychronous mex function.

4 views (last 30 days)
I am calling a windows function SetWindowsHookEx() which takes in a pointer to a function that gets called asynchronously. I'm currently passing a a function that is in the mex file to the windows function. This works well, but then I want to get that information back to Matlab. The only solution I've seen is to use writing to file, but the information is just a couple of integers, occurring perhaps once every 10 - 50 ms. Any other alternatives?

Answers (2)

Walter Roberson
Walter Roberson on 6 Oct 2016
Store the values in a static area and call into a routine that retrieves the values from the static area.
Or mexPutvariable into 'base' or 'global'
  4 Comments
Jim Hokanson
Jim Hokanson on 6 Oct 2016
Oops, I should have been more explicit. Placing doesn't, but doing something with it seems like it would. I would like to try and handle these events (run Matlab code) as they occur. Placing a variable, into the mex stack or into the Matlab workspace seems like it would require a loop somewhere to check if the value has been updated or not. I'd prefer some way of throwing the data into a queue (loosly speaking) and then signaling to Matlab that its queue is non-empty and should be handled when it can, even if the # of calls to Matlab is less than the calls to mex event callback (i.e. multiple queue events are given to Matlab at one time). Ideally this would occur in a timer or the gui event-dispatch thread.
Walter Roberson
Walter Roberson on 6 Oct 2016
If you are servicing queued events by a timer, then the timer can call in to a mex routine to retrieve the stored values.
If you were servicing events by callback, then the routine that receives the data can call the callback.
The grey area is around the question of how to get MATLAB to service the event "when it can", where "when it can" is not intended to be "immediately" and might not even be "as soon as MATLAB itself is back in control, taking into account that MATLAB might be calling into BLAS or LINPACK and so might be busy". I get the impression you are looking for something closer to "maybe on the same boundaries as a graphics callback would be serviced"... and even then, the graphics callback boundaries are not always good times for an interrupt, since graphics callbacks are not infrequently in the middle of real-time work that you cannot afford to have delayed indefinitely. I am wondering if you have thought through the circumstances under which you would like the event to occur or not.
One possibility might be if you could signal an event to an event listener. At the moment I do not see a direct way in Mex to do that. Perhaps if you used engEvalString to call notify() ? That might interrupt MATLAB temporarily "now" but would queue an event that could then be serviced by the regular MATLAB event process.

Sign in to comment.


Philip Borghesani
Philip Borghesani on 6 Oct 2016
Edited: Philip Borghesani on 6 Oct 2016
I believe that calling back into MATLAB from a windows hook with mexCallMatlab or mexEvalString works fine, or has in past versions of MATLAB. No guarantee that it will continue to be safe with future versions of MATLAB. You can then pass the mex function a function handle to execute when needed.
In any case calling a user defined function is no more likely cause problems than manipulating variables with mexPutVariable.

Categories

Find more on External Language Interfaces in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!