reaaranging filesnames

1 vue (au cours des 30 derniers jours)
Alexandros
Alexandros le 6 Déc 2011
Dear Matlabians.
I have build 2 scripts that are taking multiples xls files and and collecting data and make a final xls files.
The name of the initial files are consistent but are faulty.
The names look like. Vela2_DayMonthYear_NumberofTest_Car_TypeofFile
e.g. Vela2_01032011_001_Audi4_BAG, Vela2_01032011_002_Audi4_BAG, Vela2_25022011_001_Audi4_BAG, Vela2_25022011_002_Audi4_BAG, Vela2_28022011_001_Audi4_BAG, Vela2_28022011_002_Audi4_BAG
As my script is build and matlab is build it takes first 01032011_001 then 01032011_002 then 25022011_001 then 25022011_002 and then 28022011_001 and finally 28022011_002. This is not correct.
I want to take firstly 25022011_001 then 25022011_002 then 28022011_001 then 28022011_002 and finally 01032011_001 and 01032011_002 .
Normally people that name files should put the year then the month and then the day to name files. Put know things are wrong and I have to find a solution. I was thinking in producing a list inside my script that will take the name of the files rearrange the files by YearMonthDay_NumberofTest and link to the old files and use that. But I don't have any idea if this is a good idea or if there is a better way in doing this.
Any help will be very nice Thank you very much

Réponse acceptée

Alexandros
Alexandros le 7 Déc 2011
Your way is the write way. But I can not get the write expression for the file name inside the regexprep function. But I could be able to make an ind indicator by taking the string of the name with the date and number rearrange it in yearmonthday_number and then organize it with your code of line
[~, ind] = sort(tempnames)
If somebody can explain what [~, ind] I will be very great full. Thus ind is organize and then i can use this list for the xls rearrangement .
Thanks
  2 commentaires
Alexandros
Alexandros le 7 Déc 2011
I can not find anywhere on the internet what [~, ind] means?
I know that it makes a list but why and how do you explain that i dont get it
Is a very nice trick though. I will like to know why it does it.
Thank you
David Young
David Young le 7 Déc 2011
~ just stands for an unused result. You could replace the line with
[unused, ind] = sort(tempnames)
and it would behave just the same.
Since the regexp expression works fine for the examples you gave, you need to show what other file names you want it to work with.
If you would like more help, you could consider accepting my answer rather than your own.

Connectez-vous pour commenter.

Plus de réponses (1)

David Young
David Young le 6 Déc 2011
You can sort the filenames into the order you require like this.
First, all the filenames need to be a cell array. Their order does not matter.
filenames = { ...
'Vela2_01032011_001_Audi4_BAG'...
'Vela2_01032011_002_Audi4_BAG'...
'Vela2_25022011_001_Audi4_BAG'...
'Vela2_25022011_002_Audi4_BAG'...
'Vela2_28022011_001_Audi4_BAG'...
'Vela2_28022011_002_Audi4_BAG'};
Then you sort them by changing the names to the year/month/day ordering and calling sort. If the names don't all start 'Vela2_' you can omit it from the pattern.
tempnames = regexprep(filenames, 'Vela2_(\d\d)(\d\d)(\d\d\d\d)', 'Vela2_$3$2$1');
[~, ind] = sort(tempnames);
sortnames = filenames(ind);
Now newnames has the original filenames, but in the correct order, as we can demonstate by printing them out:
for ii = 1:length(sortnames)
disp(sortnames{ii});
end
which prints
Vela2_25022011_001_Audi4_BAG
Vela2_25022011_002_Audi4_BAG
Vela2_28022011_001_Audi4_BAG
Vela2_28022011_002_Audi4_BAG
Vela2_01032011_001_Audi4_BAG
Vela2_01032011_002_Audi4_BAG
If you use a loop to iterate over sortnames, you'll process them in date order.
  2 commentaires
Alexandros
Alexandros le 7 Déc 2011
I can not find anywhere on the internet what [~, ind] means?
I know that it makes a list but why and how do you explain that i dont get it
Is a very nice trick though. I will like to know why it does it.
Thank you
David Young
David Young le 7 Déc 2011
See my comment on your answer. ind is just a variable: to understand its role, look at the documentation for the sort() function.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by