How do i create a new .txt document for each run of a code?

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

fprintf does not create files, but writes into existing files. fopen is responsible for opening or creating files.
folder = 'C:\Temp'; % Set accordingly
for k = 1:10
filename = sprintf('file%03d.txt', k);
file = fullfile(folder, file);
[fid, msg] = fopen(file, 'w'); % Create for writing
assert(fid > 0, msg); % Error message on demand
fprintf(fid, 'This is file %s\n', k);
fclose(fid);
end
This creates 10 files called 'file001.txt' to 'file010.txt'.

3 Comments

Thank you so much!
I've got it working now.
The whole for loop is unnecessary for my use case, but I can sse myself using it in the future.
What is the point of the "file = fullfile(folder, file);", can this not be omitted?
Including the folder in a file is called "absolute path". Working with the file name only operates in the current directory. Remember, that callbacks of timers and GUIs can modify the current directory e.g. to access a file using a "relative path". This can cause unexpected collisions and an indeterministic behavior. If errors are not reproducible, the debugging is extremely hard.
Therefore it is a good programming practice to use absolute paths in every case with any exceptions.
Another reliable practice is to check the success of opening the file. fopen does not stop with an error, when it fails.
That makes a lot of sense, thank you again!

Sign in to comment.

More Answers (1)

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

I see now rereading the fopen that I missed that part of the 'permission' subchapter. Thank you for clearing it up. Definitely something to note for the future.

Sign in to comment.

Categories

Find more on Scripts in Help Center and File Exchange

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!