function to open hyperlink to last error
12 views (last 30 days)
Show older comments
Hi all,
I've run into quite a pickle of a problem, I wonder if you've got it solved around the shop already somewhere.
I am trying to implement a function (say, 'gle.m') that opens the hyperlink provided by an error throwback directly, without me having to click on it with the mouse. I've tried several approaches, but since the error messages have different formats they are different to script.
Here's something that works from the command line:
msgText = getReport(MException.last,'extended','hyperlinks','on');
hyperlinks = regexp(msgText,'<a.*?/a>','match');
stringlit = regexprep(hyperlinks{end}(18:end),'\).+','\)');
eval(stringlit)
However, I cannot get it to work from a function!
Within a function, the following works:
lasterr = lasterror;
message = lasterr.message;
% check and see if error contains a specific file
f = regexp(message,'File: \w+\.m','match');
if isempty(f)
return;
else
l = regexp(message,'Line: \d+','match');
c = regexp(message,'Column: \d+','match');
filename = f{1}(7:end);
fileloc = which(filename);
linenum = str2num(l{1}(7:end));
colnum = str2num(c{1}(9:end));
opentoline(fileloc,linenum,colnum)
end
However, it does not work for all error specifications.
This was turning out to be such an esoteric thing that I thought I might refer it to the experts. Any help appreciated.
Thanks!
0 Comments
Answers (1)
Carl
on 7 Feb 2017
Edited: Carl
on 7 Feb 2017
The documentation for "lasterror" indicates that it will be removed in a future version of MATLAB. Either way, a more robust and better documented way to handle errors is with "try, catch" blocks. See the link below for more information on using this to handle errors:
Basically, you can do something like this:
try
plot('generate error'); % invalid input
catch ME
msg = getReport(ME, 'basic')
end
When an error gets thrown in the try block, it generates an MException object, which gets passed to the catch block. The "getReport" function can obtain the full error message, including the link.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!