Home Ask Login Register

Developers Planet

Your answer is one click away!

Ahmet Tavli February 2016

Matlab Processing Sequentially Numbered Images

I have nearly 1000 images started as img0.png, img1.png, img3.png, ..., img1000.png. I am using MatLab and my code is following:

    imageNames = dir(fullfile(workingDir,'*.png')); 

So imageNames struct contains as following




img100.png .......

But I want



img2.png .......

Is this possible to do programmatically in MatLab?



Suever February 2016

If you're trying to convert the filenames like you've said in your comment. You can use regexprep to get a list of new filenames corresponding to the old filenames.

newnames = regexprep({imageNames.name},'(img)(\d*)(\.png)','$1${sprintf(''%04d'', $2)}$3')

Essentially, this pulls out the numeric part of each filename, and runs this through sprintf to zero pad it as you suggested (4 digit zero-padding).

If you want to re-save the images with these new names, you can do that simply in a loop.

for k = 1:numel(imageNames);
    movefile(imageNames(k).name, newnames{k});

If you want, you can also sort the newnames variable and it will sort naturally now.

[values, sortinds] = sort(newnames);
sortedNames = imageNames(sortinds);

Post Status

Asked in February 2016
Viewed 2,450 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life