- Polar_Sense_8C4CF322_20210901_143631_ACC.txt
- Polar_Sense_8C4CF322_20210901_143630_HR.txt
- Polar_Sense_8C4CC52B_20210901_143643_MAGN.txt
- Polar_Sense_8C4CC52B_20210901_143641_GYRO.txt
- Polar_Sense_8C4CC52B_20210901_143630_ACC.txt
- Polar_Sense_8C4CC52B_20210901_143629_HR.txt
- Polar_Sense_8C4CB82B_20210901_143644_MAGN.txt
- Polar_Sense_8C4CB82B_20210901_143642_GYRO.txt
- Polar_Sense_8C4CB82B_20210901_143630_ACC.txt
- Polar_Sense_8C4CB82B_20210901_143629_HR.txt
Renaming .txt file names to more user-friendly and consistent style
조회 수: 4 (최근 30일)
이전 댓글 표시
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:
- Polar_Sense_8C4CB82B_20210901_143629_HR
- Polar_Sense_8C4CB82B_20210901_143630_ACC
- Polar_Sense_8C4CB82B_20210901_143642_GYRO
- Polar_Sense_8C4CB82B_20210901_143644_MAGN
- Polar_Sense_8C4CC52B_20210901_143629_HR
- Polar_Sense_8C4CC52B_20210901_143630_ACC
- Polar_Sense_8C4CC52B_20210901_143641_GYRO
- Polar_Sense_8C4CC52B_20210901_143643_MAGN
- Polar_Sense_8C4CF322_20210901_143630_HR
- Polar_Sense_8C4CF322_20210901_143631_ACC
etc.
Would become:
- Chest_HR
- Chest_ACC
- Chest_GYRO
- Chest_MAGN
- RightWrist_HR
- RightWrist_ACC
- RightWrist_GYRO
- RightWrist_MAGN
- RightAnkle_HR
- 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!
댓글 수: 0
채택된 답변
Voss
2022년 2월 5일
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}.');
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]);
% 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}.')
댓글 수: 3
Voss
2022년 2월 5일
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().
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Search Path에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!