フィルターのクリア

Diary file with current date and time as time stamp

15 ビュー (過去 30 日間)
superkevin2009
superkevin2009 2016 年 9 月 13 日
コメント済み: Futurist4Science 2017 年 10 月 10 日
I've used the diary command for years, but would now like to make the filename time specific depending on the current date and current time. How can I do that? I tried searching for this answer but couldn't seem to find anything.
Something like 9-13-2016-1529.txt
When I try to make a string out of the date (and call it say dfile) and type in diary dfile, it just creates a file called dfile.

採用された回答

Fangjun Jiang
Fangjun Jiang 2016 年 9 月 13 日
編集済み: Fangjun Jiang 2016 年 9 月 13 日
dfile=[datestr(now,'mm-dd-yyyy'),'-1529.txt'];
diary(dfile);
  2 件のコメント
superkevin2009
superkevin2009 2016 年 9 月 13 日
This is a start, but how can I get the time in there as well? 15-29 was supposed to correspond to 3:29 pm (I figure colons won't work in filenames).
Steven Lord
Steven Lord 2016 年 9 月 13 日
N = datetime('now')
N.Format = 'MM-dd-yyyy-HHmm'
s = char(N)

サインインしてコメントする。

その他の回答 (2 件)

Futurist4Science
Futurist4Science 2017 年 9 月 26 日
編集済み: Futurist4Science 2017 年 10 月 10 日
I required the same functionality so I wrote a shortcut script.
% Turn On Diary Logging
diary off
% first turn off diary, so as not to log this script
diary_filename=['CommandWindowLog',datestr(now,30)];
% setup temp variable with filename + timestamp, echo off
set(0,'DiaryFile',diary_filename)
% set the objectproperty DiaryFile of hObject 0 to the temp variable filename
clear diary_filename
% clean up temp variable
diary on
% turn on diary logging
I tested it and it works!
example file name output: CommandWindowLog20170926T134629
You can also change the format of the desired timestamp per the arguments for >>datestr in the help file.
  2 件のコメント
Jan
Jan 2017 年 9 月 26 日
編集済み: Jan 2017 年 9 月 26 日
This works as expected in R2016:
diary off
filename = ['CommandWindowLog', datestr(now,30)];
diary(filename);
The wanted file is created and the diary is written to it. What do you mean by
>>diary(filename) will not pass
variables for the timestamp to work as of Matlab R2014b"
? What do you expect to passed where?
Futurist4Science
Futurist4Science 2017 年 10 月 10 日
Jan,
Thanks for the comment, I tested it again and it must have been a syntax entry issue that precipitated the comment note. I removed it.

サインインしてコメントする。


Steven Lord
Steven Lord 2016 年 9 月 13 日
Use the function form of diary. For demonstration purposes I'll use help instead of diary but the idea is the same.
fun = 'sin';
help(fun) % function form, displays help for the function whose name is stored in the variable fun
help fun % command form, displays help for the function named fun (if it exists)
help sin % command form, displays help for the function named sin

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by