rename single .txt file with changing seed number

I've created a GUI with App Designer. It's basic, four drop down menus with descriptors, and a single button that renames a .txt file
The file i'm trying to rename is spit out by a data aquisition software that names the file with a prefix and a seed number. For example:
"NewTest00001.txt", then the next save spits out "NewTest00002.txt", ecetera.
The code i've written within the ButtonPushed function is as follows:
app.DropDown1.Value;
app.DropDown2.Value;
app.DropDown3.Value;
app.DropDown4.Value;
NewTest='C:\Users\PC\Desktop\TestData\NewTest00001.txt';
movefile(NewTest, strcat('C:\Users\PC\Desktop\TestData\Renamed\',...
app.DropDown1.Value,app.DropDown2.Value,app.DropDown3.Value,...
app.DropDown4,'.txt'));
Which works when the file name matches, the problem is that the seed changes after every save. I've tried changing the code to
NewTest='C:\Users\PC\Desktop\TestData\*.txt';
But that creates a new folder within the "Renamed" folder due to the * which interprets it as a folder instead of a text file.
There's got to be a simple solution i'm unaware of where the code reads in the text file like "<NewTest><LiterallyAnyNumber>.txt" and renames it.
Essentially I run test, save test as "NewTest#####.txt", Press the rename button in my GUI, repeat.

 Accepted Answer

Do you know the seed?
testSeed = 454;
NewTest = strcat('C:\Users\PC\Desktop\TestData', num2str(testSeed), '.txt')
NewTest = 'C:\Users\PC\Desktop\TestData454.txt'
If you want the same number of digits in the seed each time:
testStr = sprintf('%05d',testSeed);
NewerTest = strcat('C:\Users\PC\Desktop\TestData', testStr, '.txt')
NewerTest = 'C:\Users\PC\Desktop\TestData00454.txt'

5 Comments

The seed # changes after every test I run.
@Garrett Craig Porter I don't have a clear idea exactly what information is available to you in Matlab, or how many of these files are in the directory. Perhaps some adaptation of the following would work:
% Assumes only one match
d = dir('NewTest*.txt');
testSeed = d.name;
NewTest = strcat('C:\Users\PC\Desktop\TestData\', testSeed);
movefile(NewTest, strcat('C:\Users\PC\Desktop\TestData\Renamed\',...
app.DropDown1.Value,app.DropDown2.Value,app.DropDown3.Value,...
app.DropDown4,'.txt'));
If you want the last file that was generated, you might try:
d = dir('C:\Users\PC\Desktop\TestData\NewTest*.txt');
[~,I] = sort({d(:).date});
testSeed = d(I(end)).name;
Thank you so much Chris! I was able to get the proper execution with the following code. I didn't even think to approach the code to sort the files that way and just grab it by date.
app.DropDown1.Value;
app.DropDown2.Value;
app.DropDown3.Value;
app.DropDown4.Value;
d=dir('C:\Users\PC\Desktop\TestData\*.txt');
[~,I]=sort({d(:).date});
testSeed=d(I(end)).name;
NewTest=(strcat('C:\Users\PC\Desktop\TestData\',testSeed));
movefile(NewTest, strcat('C:\Users\PC\Desktop\TestData\Renamed\',...
app.DropDown1.Value,app.DropDown2.Value,app.DropDown3.Value,...
app.DropDown4,'.txt'));
@Garrett Craig Porter looks good, but I'm not sure the first four lines are doing anything.
They get the values in the dropdown menus, but they don't assign the values anywhere, nor print them to the display due to the presence of the semicolons. The code should run fine without those lines, I think.

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Asked:

on 21 Sep 2022

Commented:

on 23 Sep 2022

Community Treasure Hunt

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

Start Hunting!