MATLAB Answers

Using switch case to rename files in a folder

4 views (last 30 days)
I have a folder of .png files which have a timestamp printed on them. Unfortunately, the only metadata in the .png files is the date, so I am having to manually create a timestamp for the filename of each image. To do this, I first of all numbered each .png from each day from 0001-end. For the first day of each batch of images, I can then look at which number=what time, and add that time to the filename. This is what I have done here:
%5th May 2020, Louise Wilson
%Rename files in a folder.
location=('H:\Cameras\Cameras_frames\SiteOne_rename'); %place where files to be renamed are
d=dir(fullfile(location, '*png')); %list all files in folder
files=length(d); %number of files in folder
for i=1:files
%get site
site=strsplit(location, {'\', '_'});
%get date
file=strsplit(d(i).name, {'-', '.'});
filedate=file(1); %yyyymmdd
%get number
framenumber=char(file(2)); %####.png
%get time
switch framenumber
case '0001'
case '0002'
case '0003'
case '0004'
case '0005'
case '0006'
case '0007'
case '0008'
error('Unknown time!');
newfilename=char(strcat(site, '-', filedate, timeis, '-', framenumber));
%newfilename=char(strcat(site, '-', (d(i).name)));
movefile(fullfile(location, d(i).name), fullfile (location, newfilename)); %rename file
clearvars -except location folder d files folderparts sitename
I have removed some of the times to shorten the code but the times actually cover a full day up to 2000.
My problem is that this is quite a long bit of code and also, for the many batches of images I have, the first frame number on each day is not always the same time. The first time depends on when the camera was triggered and that changes every month or so when I check the camera.
I would like to be able to say what the first frame is, and rather than then subsequently list all of the other times, just say that the second image is the first image, plus 10 minutes.
To try to explain clearly:
case '0001'
case '0002'
timeis= %case0001+10mins;
case '0003'
So, perhaps some sort of loop to increase the case number by one on each iteration, and with that add 10 mins? Can anyone help? Thanks!

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 12 May 2020
Edited: Stephen Cobeldick on 12 May 2020
Method one: datetime scalar:
Before the loop, define both the step size and the first time:
>> step = minutes(10);
>> first = datetime(0,0,0,6,0,0,'Format','HHmm') % could also include the date!
first =
The replace the entire switch with this:
timeis = char(first+step*(str2double(framenumber)-1));
Method two: datetime vector:
Before the loop, define a vector of times:
>> step = minutes(10);
>> first = datetime(0,0,0,6,0,0,'Format','HHmm');
>> dtvec = first:step:datetime(0,0,0,24,0,0); % Select the end time to suit your data
Then replace the entire switch with this:
timeis = char(dtvec(str2double(framenumber)));
  1 Comment
Louise Wilson
Louise Wilson on 13 May 2020
Thanks Stephen. I tried the first option and that nailed it! Much more efficient code and solves my problem perfectly. Thank you!

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!