MATLAB: Creating log of matlab run script with time and date log file i want to create log file with date and time. could you please help me how to write time and date information into log file. Best Answer FileName = fullfile(tempdir, 'MyLogFile.txt');Message = 'Hello';fid = fopen(FileName, 'a');if fid == -1 error('Cannot open file: %s', FileName);endfprintf(fid, '%s: %s', datestr(now, 0), Message);fclose(fid);See doc datestr for other formats.With a modern Matlab version:fprintf(fid, '%s: %s', datetime, Message); Related SolutionsMATLAB: Reading .txt file of values separated by semicolon C = textscan(fileName, '%s','Delimiter',';') MATLAB: How to create a log file It depends on what you consider a "log file" to be.It is easy to append any messages to a file:yourMsg = 'I am alive.'fid = fopen(fullfile(tempdir, 'YourLogFile.txt'), 'a');if fid == -1 error('Cannot open log file.');endfprintf(fid, '%s: %s\n', datestr(now, 0), yourMsg);fclose(fid);Copy this into a function and you can use it from where ever you want.Or perhaops you mean the diary function? See:doc diary Related QuestionHow to append a text file to another text fileCurrent date and timeWriting to a CSV file
Best Answer