5

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 ;)

Community
  • 1
  • 1
Philipp F
  • 874
  • 9
  • 29
  • What is your end goal? You want to have an other matlab process sending a sound signal *bip* whenever your first code is running into a problem? What about catch/try? – hyamanieu Mar 27 '14 at 13:46
  • Yes, I want another process to notify me when matlab is running into a problem. But I don't want to catch the error because I want matlab to stop at the error so I can inspect it. – Philipp F Mar 27 '14 at 15:16
  • There is a preference to automatically open the file when it reaches a breakpoint. It seems this is built in the architecture of Matlab itself though. – hyamanieu Mar 27 '14 at 15:36
  • How about monitoring indirectly, eg; cpu usage droping off? – RTL Mar 27 '14 at 16:32

4 Answers4

5

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.

CatsLoveJazz
  • 829
  • 1
  • 16
  • 35
  • 1
    The answer by RTL is what I needed, but this feature is the key for it to work! Thanks – Philipp F Mar 31 '14 at 07:29
  • No problem, I had thought you were looking to access this feature from another instance of MATLAB rather than just thread. If anyone knows how to do this I would be interested to know! It may be possible by knowing the instance handle from something like com.mathworks.mde.desk.MLDesktop.getInstance; – CatsLoveJazz Mar 31 '14 at 14:21
4

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

RTL
  • 3,577
  • 15
  • 24
3

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.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • Somehow I ended up with exactly this answer (started out quite differently). I will just this my vote and leave my answer as an example. – Dennis Jaheruddin Mar 28 '14 at 15:17
0

My guess is that you are looking for a way to let matlab do something when an error occurs, and still catch it. Try a setup like in the following function:

function testscript(s)
try
x=2;
x=y;
catch err
    beep %Or send a signal to an other program
    rethrow(err) %This will trigger dbstop if error
end
end

Note that when you catch the error, you can still see which value x has.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122