Renaming .txt file names to more user-friendly and consistent style

1 view (last 30 days)
Hello all,
I have been struggling to find a quick fix to my problem. I have been looking around on the MathWorks website, but I have struggled to piece things together effectively.
I have thousands of .txt files which were produced using the manufacturers standard naming convention. I wish to change the names of all these files for ease of use a later stage.
Each file name as standard contains the device name, a serial number, a timestamp, and the type of data captured e.g. 'Polar_Sense_8C4CB82B_20210901_143629_HR.txt' was recorded using a Polar Sense device, has the serial number 8C4CB82B, was recorded on Sept 1st 2021 at 14:36.29, and was used to record heart rate.
I recorded data with eight of these devices, each of which was consistently located at the same anatomical location, so for example, the device with serial number 8C4CB82B was positioned on the chest.
I have one sessions worth of captured data from each sensor within separate subfolders. What I am looking to do is change the file names within each subfolder to be identified by its anatomical location and data type only, and save the renamed data within a subsequent subfolder entitled 'Renamed Data'. Serial numbers align with body part as follows:
8C4CC52B - Right wrist
8C4D2720 - Left wrist
8C4CB82B - Chest
91029F2E - Low Back
8C4D7820 - Right thigh
8C4D0023 - Left thigh
8C4CF322 - Right ankle
91025629 - Left ankle
So, what I am hoping to do, for example, is take all of the original file names, identify the anatomical location based on serial number and data type by the appended HR, ACC, GYRO, or MAGN, and simplify it such as:
  1. Polar_Sense_8C4CB82B_20210901_143629_HR
  2. Polar_Sense_8C4CB82B_20210901_143630_ACC
  3. Polar_Sense_8C4CB82B_20210901_143642_GYRO
  4. Polar_Sense_8C4CB82B_20210901_143644_MAGN
  5. Polar_Sense_8C4CC52B_20210901_143629_HR
  6. Polar_Sense_8C4CC52B_20210901_143630_ACC
  7. Polar_Sense_8C4CC52B_20210901_143641_GYRO
  8. Polar_Sense_8C4CC52B_20210901_143643_MAGN
  9. Polar_Sense_8C4CF322_20210901_143630_HR
  10. Polar_Sense_8C4CF322_20210901_143631_ACC
etc.
Would become:
  1. Chest_HR
  2. Chest_ACC
  3. Chest_GYRO
  4. Chest_MAGN
  5. RightWrist_HR
  6. RightWrist_ACC
  7. RightWrist_GYRO
  8. RightWrist_MAGN
  9. RightAnkle_HR
  10. RightAnkle_ACC
And so on.
I only require the anatomical location and the data type. The timestamp and device name are irrelevant for my purposes.
Apologies if my explanation isn't the best. I appreciate any help I can get and will do my best to clarify what I am trying to achieve if something is unclear.
Many thanks in advance!

Accepted Answer

Voss
Voss on 5 Feb 2022
Here is an approach you can use that assumes all files are in the current working directory, renaming them while keeping them in the same directory (because I didn't quite get how the sub-folders were/should be set up - but you can modify the code).
You might change @movefile to @copyfile until you are sure the code will do the right thing (and/or make a copy of your entire directory to test on, so you don't lose your files).
subs = { ...
'8C4CC52B' 'RightWrist'; ...
'8C4D2720' 'LeftWrist'; ...
'8C4CB82B' 'Chest'; ...
'91029F2E' 'LowBack'; ...
'8C4D7820' 'RightThigh'; ...
'8C4D0023' 'LeftThigh'; ...
'8C4CF322' 'RightAnkle'; ...
'91025629' 'LeftAnkle'; ...
};
fn = dir('*.txt');
disp({fn.name}.');
{'Polar_Sense_8C4CB82B_20210901_143629_HR.txt' } {'Polar_Sense_8C4CB82B_20210901_143630_ACC.txt' } {'Polar_Sense_8C4CB82B_20210901_143642_GYRO.txt'} {'Polar_Sense_8C4CB82B_20210901_143644_MAGN.txt'} {'Polar_Sense_8C4CC52B_20210901_143629_HR.txt' } {'Polar_Sense_8C4CC52B_20210901_143630_ACC.txt' } {'Polar_Sense_8C4CC52B_20210901_143641_GYRO.txt'} {'Polar_Sense_8C4CC52B_20210901_143643_MAGN.txt'} {'Polar_Sense_8C4CF322_20210901_143630_HR.txt' } {'Polar_Sense_8C4CF322_20210901_143631_ACC.txt' }
c = split({fn.name}.','_');
c(:,[1 2 4 5]) = [];
[ism,idx] = ismember(c(:,1),subs(:,1));
fn(~ism) = [];
c = strcat(subs(idx(ism),2),'_',c(ism,2));
% showing how the renaming will go:
disp([{fn.name}.' c]);
{'Polar_Sense_8C4CB82B_20210901_143629_HR.txt' } {'Chest_HR.txt' } {'Polar_Sense_8C4CB82B_20210901_143630_ACC.txt' } {'Chest_ACC.txt' } {'Polar_Sense_8C4CB82B_20210901_143642_GYRO.txt'} {'Chest_GYRO.txt' } {'Polar_Sense_8C4CB82B_20210901_143644_MAGN.txt'} {'Chest_MAGN.txt' } {'Polar_Sense_8C4CC52B_20210901_143629_HR.txt' } {'RightWrist_HR.txt' } {'Polar_Sense_8C4CC52B_20210901_143630_ACC.txt' } {'RightWrist_ACC.txt' } {'Polar_Sense_8C4CC52B_20210901_143641_GYRO.txt'} {'RightWrist_GYRO.txt'} {'Polar_Sense_8C4CC52B_20210901_143643_MAGN.txt'} {'RightWrist_MAGN.txt'} {'Polar_Sense_8C4CF322_20210901_143630_HR.txt' } {'RightAnkle_HR.txt' } {'Polar_Sense_8C4CF322_20210901_143631_ACC.txt' } {'RightAnkle_ACC.txt' }
% do the renaming:
cellfun(@(x,y)movefile(x,y),{fn.name}.',c);
% check the new files' names:
new_fn = dir('*.txt');
disp({new_fn.name}.')
{'Chest_ACC.txt' } {'Chest_GYRO.txt' } {'Chest_HR.txt' } {'Chest_MAGN.txt' } {'RightAnkle_ACC.txt' } {'RightAnkle_HR.txt' } {'RightWrist_ACC.txt' } {'RightWrist_GYRO.txt'} {'RightWrist_HR.txt' } {'RightWrist_MAGN.txt'}
  3 Comments
Voss
Voss on 5 Feb 2022
I'm glad it's working!
It's good that you changed the dir('*.txt') line to return only the relevant file names, because if the file names don't all contain the same number of '_' delimiters, you'll run into an error with split().
Thomas Swain
Thomas Swain on 5 Feb 2022
Indeed, that is exactly what happened on my first attempt at running it, but now it works perfectly!

Sign in to comment.

More Answers (0)

Categories

Find more on Search Path in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!