Azzera filtri
Azzera filtri

Changing file name in matlab

1 visualizzazione (ultimi 30 giorni)
Tanmoyee Bhattacharya
Tanmoyee Bhattacharya il 4 Mar 2016
Risposto: Guillaume il 4 Mar 2016
I have multiple text files. They are data_23_45.23, data_34.56_56.56, data_23_45
I want to change names and they are data_23.00_45.23, data_34.56_56.56, data_23.00_45.00
How can I change file name.

Risposte (2)

Walter Roberson
Walter Roberson il 4 Mar 2016
You can rename() individual files.
I do not see any pattern in your renaming so I cannot offer code to carry out the task in general.

Guillaume
Guillaume il 4 Mar 2016
If I understood correctly:
%get list of files
folder = 'c:\somewhere\on\a\filesystem\';
files = dir(fullfile(folder, '*.txt'));
filenames = {files.name}
%find files with incomplete names
filepattern = regexp(filenames, '(data_\d+)(\.\d+)?(_\d+)(\.\d+)?(.*)', 'tokens', 'once');
isdatafile = cellfun(@numel, filepattern) == 5;
filenames(~isdatafile) = []; %eliminate files that don't conform to the basic pattern
filepattern(~isdatafile) = [];
filepattern = vertcat(filepattern{:}); %concatenate file patterns into an nx5 cell array
hasmissingpattern = cellfun(@isempty, filepattern); %find missing entries
filepattern(hasmissingpattern) = {'.00'};
%only rename files whose name has changed
filenames = filenames(any(hasmissingpattern, 2));
filepattern = num2cell(filepattern(any(hasmissingpattern, 2), :), 2);
%rename files
cellfun(@(old, new) movefile(fullfile(folder, old), fullfile(folder, strjoin(new, ''))), filenames', filepattern);

Categorie

Scopri di più su Historical Contests in Help Center e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by