How do i create a new .txt document for each run of a code?
Show older comments
I am attampting to code the path of a laser, and want to save the parameters and path afterwards for documentation and reference.
I am however unable to find a command capable of creating a new document. I've read some answers alluding to fprintf being capable of doing so, but i dont see this option when reading the documentation.
To my knowledge fprintf can only overwrite an existing file, and it would be bothersome to have to back up the document for each run.
Am i misunderstanding something?
Accepted Answer
More Answers (1)
Walter Roberson
on 8 Nov 2022
1 vote
fprintf() can never create files, only write to files that are already opened.
fopen() with 'w' permission will create a file if it did not exist. If the file already exists then 'w' will keep the file attributes (such as security access) but will erase the contents of the file.
fopen() with 'a' permission will create a file if it does not exist. If it already exists then 'a' will open the file and position to the end of the file ready to append more.
There are some modifiers beyond what I list here.
If you want to be sure that you create a file name that does not already exist, you can use functions such as sprintf() or compose() to build a file name. You can use dir() to find out which files are already there.
1 Comment
Rasmus Skjødt
on 8 Nov 2022
Categories
Find more on Scripts 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!