필터 지우기
필터 지우기

How to add a variable to a filename while renaming it

조회 수: 26 (최근 30일)
Anantha Padmanabhan
Anantha Padmanabhan 2017년 3월 7일
댓글: Anantha Padmanabhan 2017년 3월 7일
I have a code that reads a bunch of .text files which are of the format "experiment_date_time" which is created by a measurement device. So a typical file name would look like
"Experiment_yyyymmdd_hhmmss.txt"
Sometimes due to an error in my device internal clock, the measurement are made at weird seconds and not 00 and this a problem when I try to load the files. My question is how do I rename a filename for just the last 2 characters? I tried this so far,
files=dir(''); for the file location
%run each file through loop and rename
for i=1:length(files)
[pathname,filename,extension] = fileparts(files(i).name);
filename=filename(1:end-2)
how do I append two zeros to the file name?
Here I want to read the file name and replace the last two characters with zero. How do I do this?
Thanks, Ananth

채택된 답변

Stephen23
Stephen23 2017년 3월 7일
편집: Stephen23 2017년 3월 7일
Untested, but this should get you started:
ptx = ''; % directory path
S = dir(fullfile(ptx,'*.txt'));
for k = 1:numel(S)
[~,old,ext] = fileparts(S(k).name);
new = sprintf('%s00',old(1:end-2));
fpo = fullfile(ptx,[old,ext]);
fpn = fullfile(ptx,[new,ext]);
movefile(fpo,fpn)
end
  댓글 수: 1
Anantha Padmanabhan
Anantha Padmanabhan 2017년 3월 7일
Hi, the code seems to work and the only change i had to make was to create a new directory to save the new files in
fpo = fullfile(ptx,[old,ext]);
fpn = fullfile(ptx1,[new,ext]);
movefile(fpo,fpn)
Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by