function to open hyperlink to last error
조회 수: 8 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Carl
2017년 2월 7일
편집: Carl
2017년 2월 7일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!