Matlab exception sent by mail is not showing the line of the error - matlab

In my matlab script I am sending a mail to me with an exception that tells me, when an error happens, which kind of error it is.
The problem I am facing is the fact that the ME exception is not showing me where the error happens (which line and which part of the code) as matlab usually does. I can't see the error in the matlab terminal too (the program just stops running). The code that sends the mail with the error is below:
try
% my script which can fail....
demo
catch ME
% An error will put here.
errorMessage = sprintf('Error in demo. The error is: %s', ME.message);
%this function just sends the mail
sendmail2me(errorMessage);
What I missed?

The exception ME is an MException object which contains an identifier, the message, a cause and the stack. The identifier is only there to allow MATLAB an unique identification of an error. The message contains a description of the error.
The cause contains an array of MExceptions which have led to the current exception. This allows you to track the exceptions to find the root of your error. As the cause is a (possibly empty) array of MException objects, you could go through the cause in an array and write the information into the mail.
Most important for you is the stack. It is a struct containing three fields: file, name and line. File is the full path to the file/function where the error occurred. Name is (obviously) the name of the file and in line (again obviously) the line where the exception occurred is saved. The stack can also be an array if the error occurred in a function called from you function/script. It would therefore be best to go through stack in a for loop, and concatenate the error message and the contents of the stack.
try
demo;
catch ME
errormsg = sprintf('%s\n',ME.message);
for k=1:length(ME.stack)
errormsg = sprintf('%s\nError in %s (line %d)\n', ...
errormsg,ME.stack(k).name,ME.stack(k).line);
end
sendmail2me(errormsg);
end
You find more detailed information on Exceptions in the MATLAB help pages.

Related

Adding a debug point as soon as an error appears MATLAB

this might be silly, Is there any way to automatically place a debug point when an error pops up in MATLAB
Error popping is done manually by
msg = 'Error has occurred'
error(msg)
What I need is, to automatically put a debug (not by manual intervention) after popping this error.
You can use dbstop with a condition:
dbstop if error
MATLAB pauses at any line in any file when the specified condition
occurs.

GWT Error Popup

The error popup with the following error message comes on the screen repeatedly when application is in idle state (no user activity is performed).
Error occurred on client: (TypeError): Unable to get property 'iterator_0' of undefined or null reference.
number: -2146823281
at handleEvent_206....EF34544...cache.html
at dispatchEvent_0..EF34544...cache.html
at sucess_184 ..
..
Can anyone give some pointers to navigate to the problamatic area in the code?
The fact that you're getting it repeatedly is probably due to the fact that you're performing an action on a timer (i.e. perform repeatedly an action).
From the small snippet you've shown, I don't think there's anything we can deduce. Do you have a larger stacktrace? It is still possible the error is in your own code (trying to invoke iterator() on a null object).

Can't process quickfix messages written in file

In my C++ QuickFix application, I am recording all MarketDataIncrementalRefresh messages i am getting into a file. This is being done using the following code:
void Application::onMessage(const FIX44::MarketDataIncrementalRefresh& message, const FIX::SessionID&)
{
ofstream myfile("tapedol.txt", std::ios::app);
myfile << message << endl << endl;
}
This part's working just fine. The problem occurs when I try to load the message later on.
FIX::Message msg
ifstream myfile("tapedol.txt");
getline(myfile,aux);
msg = aux;
msg.getField(55);
The program crashes every time it executes the last line. I suspect the problem is at the assignment to msg, but i'm not sure. If it is, what is the correct way to do such assignment? If not, how can I process the data within tapedol.txt, so that a message of type MarketDataIncremental refresh would be generated for each string in the file?
Your question is not complete enough to provide a full answer, but I do see one red flag:
msg.getField(55);
The Symbol field is not a top-level field of MarketDataIncrementalRefresh (it's inside the NoMDEntries repeating group), so this line will fail. I think it would raise a FieldNotFound exception.
My C++ is rusty, but you should be able to catch an exception or something that should tell you exactly what line is erroring out. Barring that, you need to open up a debugger. Just saying "it crashed" means you quit looking too soon.

How to deal with expired URL links when using imread URL?

I am working on the face scrub dataset, which has a long list of urls of pictures.
I used a for loop to fetch these photos. However, some of the urls had expired so my matlab code return an error saying 'Unable to determine the file format.' However I think the actual reason is that the url link does not have the image anymore. For example, one of the bad urls is:
http://www.gossip.is/administrator/components/com_n-myndir/uploads/16de47418d69b1c2991731dffaca8a78.jpg
How do I identify and ignore this error so my code can keep working on the rest of the list? I could use R instead if that make solving this problem easier.
You can implement a try/catch block to catch (original isnt'it) the error message and skip the image if the link is indeed broken.
When we use the following syntax:
try
A = imread('http://www.gossip.is/cgi-sys/suspendedpage.cgi');
catch ME
%// Just so we know what the identifier is.
ME
end
Matlab first tries to read the image given by the url. If it can't, we ask it to catch the error message (MException actually) and perform some other appropriate action.
The thing is, we need to know what is the exact error message in order to recognize it in the try/catch block.
When I entered the above code, I got the following structure for ME:
ME =
MException with properties:
identifier: 'MATLAB:imagesci:imread:fileFormat'
message: 'Unable to determine the file format.'
cause: {0x1 cell}
stack: [2x1 struct]
Therefore, once we know the exact identifier generating the error we can use strcmp to look for it in the try/catch block. For instance with the following code:
clear
clc
try
A = imread('http://www.gossip.is/cgi-sys/suspendedpage.cgi');
catch ME
if strcmp(ME.identifier,'MATLAB:imagesci:imread:fileFormat')
disp('Image link broken')
end
A = imread('peppers.png');
end
imshow(A);
Matlab displays 'Image link broken' and reads peppers.png, as expected.
Hope that helps!

Object is Invalid or Deleted when pressing button and plotting

Please help. The error says that the object is deleted.
Error using handle.handle/get
Invalid or deleted object.
Error in C:\Program Files\MATLAB\R2011b\toolbox\matlab\graph2d\plotyy.p>localUpdatePosition (line 373)
Error in C:\Program Files\MATLAB\R2011b\toolbox\matlab\graph2d\plotyy.p>#(obj,evd (localUpdatePosition(obj,evd,ax(1),ax(2))) (line 212)
I really do not understand what was deleted but every time I perform the impz(signal), the plot isn't showing the correct graph unlike the plot in freqz(signal) and zplane(signal).
This is what really happens (26seconds video) - https://www.youtube.com/watch?v=HTR45lNIjuc
Line 212 contains this code handles.N = str2num(get(handles.edtOrder,'String'));
Line 373 contains this code zplane(handles.axPlots,handles.firVector,1)
Code for impulse response
if (get(handles.cbImpResp,'Value') == 1)
set(handles.txtPlotAvail,'Visible','off');
switch (handles.filterValue)
case 'FIR'
impz(handles.axPlots,handles.firVector,1)
case 'IIR'
impz(handles.axPlots,handles.vectorB,handles.vectorA)
case 'Multiple Bandpass'
end
else
set(handles.txtPlotAvail,'Visible','on');
end
The code is either mixing up your handles or overwriting old ones. There no way to debug this without the full source.
See http://www.mathworks.com/matlabcentral/answers/85117