how to rename images (.bmp) merging information from another file?

3 次查看(过去 30 天)
Hello, I am struggling writing a code to rename some images (.bmp). I have 900 images to rename; each of one has a different name. Names can be divided into 4 categories: Stim_HF_1.bmp, Stim_HF_1_RUL.bmp, Stim_LF_1.bmp, Stim_LF_1_RUL.bmp with the number changing for every image.
I need to rename each image adding the word that corresponds to that number. For example HF_1 corresponds to the word ‘desert’, while LF_1 corresponds to the word ‘safari’. Following what I would like to obtain: ‘Stim_HF_1.bmp’ into ‘HF_1_desert.bmp’ ‘Stim_HF_1_ RUL.bmp’ into ‘HF_1_desert_ RUL.bmp’ ‘Stim_LF_1.bmp’ into ‘LF_1_safari.bmp’ ‘Stim_LF_1_ RUL.bmp’ into ‘LF_1_safari_ RUL.bmp’
I have a separate file with 3 columns and 901 (headings included) rows. The columns represent number, HF, LF. In each row there is number and words HF and LF associated (e.g. 1, desert, safari).
I wonder if there is a way to rename each image merging the original filename with the information included in the file. Do you have any idea about how to proceed? Many thanks in advance!

回答(1 个)

Guillaume
Guillaume 2015-1-9
编辑:Guillaume 2015-1-9
How about:
cd('path\to\bmpfiles');
renametable = readtable('renamefile.txt');
rowfun(@renamefile, renametable);
And in separate m-file
function renamefile(index, HFname, LFname)
sources = strrep({'Stim_HF_*.bmp', 'Stim_HF_*_RUL.bmp', 'Stim_LF_*.bmp', 'Stim_LF_*_RUL.bmp'}, '*', sprintf('%d', index));
destinations = [strrep({'HF_*.bmp', 'HF_*_RUL.bmp'}, '*', sprintf('%d_%s', index, HFname)), strrep({'LF_*.bmp', 'LF_*_RUL.bmp'}, '*', sprintf('%d_%s', index, LFname))];
cellfun(@movefile, sources, destinations);
end
  4 个评论
Susan Lane
Susan Lane 2017-5-28
Very nice answer Guillaume, thank you for the tip! Do you also have a tip on how a .bmp file can be opened and edited? I started learning to code, I am a big Windows user, and I would like to know if there is a way to do this: http://www.coreldraw.com/en/pages/bmp-file/ in code ? Thanks!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by