Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab custom dbstop handler

I know people have discussed how to make custom dbstop conditions, (such as in Customize dbstop in MATLAB)

However, I am using the normal dbstop if error and I want to know (from another process) whether a matlab process is currently in the debugging state (K>>) or normally running.

I could do this if I had a custom dbstop handler function. But I still want to be able to do hands-on debugging as with the normal dbstop if error.

If there are other possibilities to detect the state of matlab from outside (>> vs K>>), I am also happy!

Let me know any idea ;)

like image 695
Philipp F Avatar asked Mar 24 '14 09:03

Philipp F


3 Answers

This command allows you to check the debug status of the current instance:

feature('IsDebugMode')

For example:

K>> feature('IsDebugMode')
ans =
     1
>> feature('IsDebugMode')
ans =
     0
>>

I don't think this necessarily answers your question fully as you will need to access this though a different process but I hope that this is helpful all the same.

Beware: This is an undocumented feature so may disappear or change behavior between versions.

like image 105
CatsLoveJazz Avatar answered Oct 23 '22 16:10

CatsLoveJazz


Timers in matlab can spawn a seperate thread to wait in which can get around the problem of needing to look from outside the current matlab instance. We can set the timer to check if debug mode is active and if it is to do something.

A example function to check if debug mode is active and if so to do something:

function mycallbackfunction(~,~)
        if feature('IsDebugMode') % undocumented thanks to CatzLoveJazz

        load handel
        sound(y,Fs)

The two previous lines are an attention grabbing example, other possibilities are to use beep, to write to a file, or run any commands or function.

        evalin('base','stop(timerHandle)') % stop the timer
    end
end

This function could be modified to evaluate the 'attention grab' once and then reset once debug mode is no longer active. Currently it relies on just stopping and then manually restarting the timer.

(note: a previous version had an else however this was redundant as it will not run while the workspace is busy)

Now to create the timer object.

timerTic=4; % how often the timer checks

timerHandle = timer();
timerHandle.startDelay = timerTic;
timerHandle.Period = timerTic;
timerHandle.ExecutionMode = 'fixedRate';
timerHandle.TasksToExecute = inf;
timerHandle.TimerFcn = @mycallbackfunction;

and to start to timer call

start(timerHandle)

The timer will automatically stop after running the attention grabbing lines. If debug mode is never entered the timer will keep running and will need to be stoped manually with stop(timerHandle)

Remember to run delete(timerHandle) once finished to remove the object before clearing the timerHandle variable

like image 27
RTL Avatar answered Oct 23 '22 15:10

RTL


I don't know of a way to achieve exactly what you're asking for.

However, perhaps you could:

  1. Catch the exception (in a try-catch block).
  2. Within the catch block, initiate whatever external process you want to, or send a message to it, indicating that MATLAB is having a problem (perhaps including some details of the caught exception in the message).
  3. Immediately rethrow (or throw, or throwAsCaller) whatever exception was caught.
  4. Use dbstop if error to enter debug mode only on the rethrown error.

In this way you should be able to get an external process to notify you of an error, and still enter debug mode to examine it.

like image 26
Sam Roberts Avatar answered Oct 23 '22 16:10

Sam Roberts