Using a String Variable to Name a Diary File

36 views (last 30 days)
M
M on 12 Oct 2014
Commented: Bill Tubbs on 20 Apr 2020
I would like to be able to create a Log file with a different name whenever I want to. I would like to do this using the current time.
DiaryName = strcat('Paper_',datestr(now,'yyyy_mm-dd_HH:MM:SS')); diary(DiaryName.log)
However, I get an error when this happens:
"Attempt to reference field of non-structure array."
I also tried making the '.log' part of DiaryName:
DiaryName = strcat('Paper_',datestr(now,'yyyy_mm-dd_HH:MM:SS'),'.log') diary(DiaryName)
I tried also diary('DiaryName'), but this created a file called 'DiaryName' instead of giving it the name with the time stamp that I wanted to give it.
Any thoughts on what I can do?
In addition, I have directed Matlab to change paths to the directory where my .m files are (the Main directory). I have created a folder called "Diary" within the Main folder. Is it possible to create a Macros in Matlab to avoid having to specify the whole path for the folder Diary? Specifically, it would be great if I could make a Macro called DiaryPath, and the do diary($DiaryPath\'DiaryName'.log) to save the diary there?
Thanks a lot!

Answers (2)

Jan
Jan on 12 Oct 2014
Edited: Jan on 12 Oct 2014
Is it clear, why "DiaryName.log" must fail? The code means, that the field "log" of the struct "DiaryName" is wanted, but DiaryName is a string.
What is the problem with this commands:
DiaryName = strcat('Paper_', datestr(now,'yyyy_mm-dd_HH:MM:SS'), '.log');
diary(DiaryName)
Under Windows the ':' is not allowed in path names.
Instead of a macro let "DiaryPath" be a function, which replies the wanted folder:
function P = DiaryPath
P = 'C:\YourFolder\';
You have tried
diary(DiaryName)
and
diary('DiaryName')
This is pure guessing. Better read the Getting Started chapters of the documentation an rely on educated guessing only.

Seo Woo Park
Seo Woo Park on 18 Oct 2019
diary(strcat('Paper_',datestr(now,'yyyy_mm-dd_HH:MM:SS')))
might be work

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!