fileID = fopen('text\test.txt','w');
fprintf(fileID,'Comments:');
fclose(fileID);
Hi,
What can I do to not overwrite the previous file
There is a way for every time I run the script to make another file and not rewritte the old one, like test1.txt, test2.txt and so on?

 Akzeptierte Antwort

Voss
Voss am 20 Mai 2022

0 Stimmen

Here's a function (get_unused_file_name) you can use for this purpose. It takes a file name and check if that file already exists. If it does, the function appends '1' to the name and checks if that exists. If it does, it tries '2', and so on until it gets one that doesn't already exist, and returns that unused file name.
Here's a demonstration of it:
fn_orig = fullfile(pwd(),'text','test.txt') % file name to try
fn_orig = '/users/mss.system.Cr4l4C/text/test.txt'
mkdir('text')
% calling the function 4 times
for ii = 1:4
fn = get_unused_file_name(fn_orig)
% create the file so that next time around
% this file name is already taken:
fid = fopen(fn,'w');
fclose(fid);
end
fn = '/users/mss.system.Cr4l4C/text/test.txt'
fn = '/users/mss.system.Cr4l4C/text/test1.txt'
fn = '/users/mss.system.Cr4l4C/text/test2.txt'
fn = '/users/mss.system.Cr4l4C/text/test3.txt'
function file_name = get_unused_file_name(file_name)
[pn,fn,ext] = fileparts(file_name);
ii = 0;
while isfile(file_name)
ii = ii+1;
file_name = fullfile(pn,sprintf('%s%d%s',fn,ii,ext));
end
end

7 Kommentare

Cristian Martin
Cristian Martin am 20 Mai 2022
Thank you for answer, I'll will apply and come back to you for feedback!
Cristian Martin
Cristian Martin am 20 Mai 2022
fn_orig = fullfile(pwd(),'text','RT.txt'); % file name to try
mkdir('text');
for ii = 1:4
fn = newname(fn_orig);% create the file so that next time around this file name is already taken
fid = fopen(fn,'w');
fclose(fid);
end
After this part i receive this:
> In get_unused_file_name (line 3)
Attempt to execute SCRIPT get_unused_file_name as a function:
C:\Users\NewFolder\get_unused_file_name.m
Error in get_unused_file_name (line 5)
fn = get_unused_file_name(fn_orig);
and if I change get_unused_file_name in newname:
Warning: Directory already exists.
> In get_unused_file_name (line 3)
Undefined function or variable 'newname'.
Error in get_unused_file_name (line 5)
fn = newname(fn_orig);
Voss
Voss am 20 Mai 2022
Bearbeitet: Voss am 20 Mai 2022
Copy and paste the function get_unused_file_name (exactly as it is in my answer, including the function line) to a new m-file. Save the m-file as "get_unused_file_name.m".
Then you can use it like this:
fn = get_unused_file_name(fn_orig);
where fn_orig is the file you want to write to, and fn is the name of the actual file that will be used. Then use fn in your subsequent fopen call.
Cristian Martin
Cristian Martin am 20 Mai 2022
File get_unused_file_name:
function file_name = get_unused_file_name(file_name)
[pn,fn,ext] = fileparts(file_name);
ii = 0;
while isfile(file_name)
ii = ii+1;
file_name = fullfile(pn,sprintf('%s%d%s',fn,ii,ext));
end
end
Other file:
fn_orig = fullfile(pwd(),'text','test.txt'); % file name to try
for ii = 1:4
fn = get_unused_file_name(fn_orig);
fid = fopen(fn,'w');
fclose(fid);
end
fileID = fopen('fn','w','n');
fprintf(fileID,'Comments:');
fclose(fileID);
The error:
Undefined function or variable 'isfile'.
Error in get_unused_file_name (line 4)
while isfile(file_name)
Error in Untitled7 (line 4)
fn = get_unused_file_name(fn_orig);
I'll try again tomorrow, I guess I'm too tired :)
Looks like isfile was introduced in R2017b. Your version of MATLAB must be older than that. Try replacing that line:
while isfile(file_name)
with this:
while exist(file_name,'file')
And, by the way, in the "other file", you don't need to have the loop - that was just for demonstration to show how the function works. Going by the code in your original question, you can do something like this:
% fn_orig is the full path to test.txt, in directory
% \text, a directory which must already exist under
% the current working directory ( pwd() ):
fn_orig = fullfile(pwd(),'text\test.txt');
% fn is the file that will be written to:
fn = get_unused_file_name(fn_orig);
% write the file:
fileID = fopen(fn,'w');
fprintf(fileID,'Comments:');
fclose(fileID);
Cristian Martin
Cristian Martin am 20 Mai 2022
Indeed I use 2015a, and with exist its perfect functional. Now I get the ideea, but still I'm tired. Thank you for your patience and kindness!
Voss
Voss am 20 Mai 2022
You're welcome! Sorry about the snag with isfile, but I'm glad it's working now!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2015a

Gefragt:

am 20 Mai 2022

Kommentiert:

am 20 Mai 2022

Community Treasure Hunt

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

Start Hunting!

Translated by