If you have a complicated project with many functions interconnected with each other, debugging could be a pain in the neck. To make it a bit more easier, I personally write "current status" of the code to a text file.
Every time a specific event is caused, I write to the text file, along with the timestamp of the message. This helps me often, to pin point the mistake in my code.
This is fairly simple thing to do. See the below function, write_log.m
pwd stands for present working directory. I want to save the LogFile in the same directory as the current project.
Every time the message, indicated by the variable msg here, is written to the text file, I precede it with the current date and time.
Lets make a few example calls to the function and see what the text file contains.
Taking a look at the LogFile, we see the following three lines:
Notice the timestamps and their delays from each other.
Every time a specific event is caused, I write to the text file, along with the timestamp of the message. This helps me often, to pin point the mistake in my code.
This is fairly simple thing to do. See the below function, write_log.m
function write_log(msg)
fid =
fopen(fullfile(pwd, 'LogFile.txt'), 'a');
if fid == -1
error('Cannot open log
file.');
end
fprintf(fid,
'%s: %s\r\n', datestr(now, 0),
msg); %this line could be changed to add more details to the message
fclose(fid);
end
Every time the message, indicated by the variable msg here, is written to the text file, I precede it with the current date and time.
Lets make a few example calls to the function and see what the text file contains.
write_log('starting the project');
pause(2); %wait 2
seconds.
write_log('No error yet');
pause(1); %wait 1 second.
write_log('Alright, time to see the results!');
12-Jan-2020 19:51:12: starting the project
12-Jan-2020 19:51:14: No error yet
12-Jan-2020 19:51:15: Alright, time to see the results!
Comments
Post a Comment