How to truncate/modify a file name

조회 수: 8 (최근 30일)
Sid
Sid 2012년 2월 13일
Hi everyone,
The question pertains to modifying file names during a batch processing operation. Basically, I would like to truncate a certain portion of the file name and use that as the output name of the processed file. Please see the code below and the comments, outlining the question:
clc
clear all
files = dir('*.JPG');
filePrefix_cr = 'Cropped ';
% For example, let's use a file name like: S-0281 AB196C3 View 3.jpg.
% What I would like to have for a file name is: Cropped AB196C3 View 3.jpg.
% The result from this code is: Cropped S-0281 AB196C3 View 3.jpg.
% How do I truncate the S-0281, i.e. the first 6 characters of the file
% name, prior to introducing 'Cropped'?
for k = 1:numel(files)
rgb = imread(files(k).name);
% Runs a file modification process, e.g. a cropping process.
imwrite(rgb, [filePrefix_cr files(k).name]);
end
Thanks in advance for all the help and have a good week.
Regards

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 13일
imwrite(rgb, [filePrefix_cr files(k).name(7:end)]);

추가 답변 (2개)

Jeff E
Jeff E 2012년 2월 13일
This will truncate the first six chars. If the file name part you want to truncate changes in size, check out regexp to find, say, the first space in your string.
tname = 'S-0281 AB196C3 View 3.jpg':
tname2 = tname(7:end);

Sid
Sid 2012년 2월 13일
Thank you both for your answers!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by