Renaming output files so as not to overwrite when name is duplicated

19 ビュー (過去 30 日間)
Stephnie Watson
Stephnie Watson 2020 年 12 月 22 日
回答済み: Steven Lord 2023 年 11 月 25 日
Hello Community!
It has been a couple of years since I have coded in MATLAB and I am a bit rusty.
I am currenlty working with data that is being output with the final date on the file. Unfortunately this can lead to duplications occiring. If I remember correctly there is a way to write the code so that it scans the names already in the file folder and would add an additional variable to the name so that it does not overwrite the existing file.
I am thinking it is an if statement that does that? Is that correct or is there a better way to write that?
ex) File in folder is already labelled 202004, would like any additional files that have the same last date to be output as 202004B, etc. Most of the files are uniques but there are a couple where this happens.
Thanks!
  4 件のコメント
Stephnie Watson
Stephnie Watson 2020 年 12 月 23 日
編集済み: Rik 2020 年 12 月 23 日
Thanks Rik and Steve I will read up on this one!

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

回答 (2 件)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 12 月 22 日
You can use some code like this:
filename = '202004';
n=0;
if ~isempty(dir(filename))
for n=1:1000
newFile = fprintf('%s_%03d\n',filename,n);
if isempty(dir(newFile))
break;
end
end
end
% save the date on newFile
  1 件のコメント
Stephnie Watson
Stephnie Watson 2020 年 12 月 23 日
Thank you Asad I will give this a try too. Much appreciated!

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


Steven Lord
Steven Lord 2023 年 11 月 25 日
There is a function named matlab.lang.makeUniqueStrings that may be of use.

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by