From: LAN on
Hello everyone, I had a question about using the diary function to catch error code in a GUI created with GUIDE. I am testing how to capture error code that is output in the command window.

I set up the GUI with a single pushbutton that would execute the code below. It is then supposed to save the command window text to a file called diarytext. However, when I open the text file it is always empty.

This is my code:

function pushbutton1_Callback(hObject, eventdata, handles)
diary diarytext.txt
try
A = rand(5,3); B = rand(4,5);
C = [A, B];
catch exception;

diary('off')
edit('diarytext.txt')
throw(exception);

end

If someone could lend a hand with this problem, I'd be very appreciative. Thanks!
From: Michael on
FID = fopen('errorlog.txt','a');
if FID>-1
fprintf(FID,'%s\n',getReport(MException.last));
fclose(FID);
end;
From: LAN on
"Michael " <michaelrobbinsusenet(a)aol.com> wrote in message <i1lh12$qo2$1(a)fred.mathworks.com>...
> FID = fopen('errorlog.txt','a');
> if FID>-1
> fprintf(FID,'%s\n',getReport(MException.last));
> fclose(FID);
> end;

Thanks for your reply, Michael, however I don't know what these commands mean or how they fit into a try-catch block, which is where it needs to be. Can you elaborate on how it would work with my code, or provide a bit more context for these commands?
From: LAN on
"LAN " <dsiewem(a)gmail.com> wrote in message <i1n4o5$okt$1(a)fred.mathworks.com>...
> "Michael " <michaelrobbinsusenet(a)aol.com> wrote in message <i1lh12$qo2$1(a)fred.mathworks.com>...
> > FID = fopen('errorlog.txt','a');
> > if FID>-1
> > fprintf(FID,'%s\n',getReport(MException.last));
> > fclose(FID);
> > end;
>
> Thanks for your reply, Michael, however I don't know what these commands mean or how they fit into a try-catch block, which is where it needs to be. Can you elaborate on how it would work with my code, or provide a bit more context for these commands?

I spent a little more time on it and was able to figure it out.

This was my final code:

try
A = rand(5,3); B = rand(4,5);
C = [A, B];

catch ME;
report = getReport(ME);
FID = fopen('errorlog2.txt','a');
fprintf(FID,'%s\n',report);
fclose(FID);


end

Much appreciated!