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

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!

Answers (1)

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 Comments

Hello Guillaume, Thank you for your answer! However, I'm still struggling. I'm new to Matlab, so it may be that I'm missing something obvious. I'm stuck at the 3rd line of the code. This is the error that appears:
"Undefined function or variable 'renamefile'.
Error in RenamingImages2 (line 7) rowfun(renamefile, renametable)"
The code that I used is the following: cd('with\my\path\to\bmp\files');
renametable = readtable('FileForMatlab.xlsx');
rowfun(renamefile, renametable);
Note, I changed the file from .txt to .xlsx because otherwise number, HF and LF were not separated into 3 different columns. Anyway, I tried also with .txt and the 3rd line of the code gives the same error.
Thanks.
You obviously haven't created the separate m-file (renamefile.m) with the content stated in my answer, or you haven't put it somewhere in your matlab path.
If readtable doesn't read your text file properly, it's most likely because it's expecting a different delimiter. You can change the delimiter with:
renametable = readtable('renamefile.txt', 'Delimiter', '\t');
See the documentation of readtable for more.
Or maybe the problem is that you've remove the @ before renamefile
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!

Sign in to comment.

Categories

Tags

Asked:

on 9 Jan 2015

Commented:

on 28 May 2017

Community Treasure Hunt

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

Start Hunting!