Bilal
Bilal

Reputation: 3864

Problem adding tow digits to path string to read multiple files

I want to read multiple files from a folder, the only problem in my code down is that instead of having the full image path:

'checkboard/Gaussian_Noised/image_01.jpg'

I have the path without the zero in the last two digits

EDIT: There is space instead of zero on the first 9 Image paths.

'checkboard/Gaussian_Noised/image_ 1.jpg'

how can I fix this please

%-------------------------------------------------------
clc
clear all
close all
%------------------------------------------------------
path = 'checkboard/Gaussian_Noised/';
% Define images to process
imageFileNames = cell(1,36);
for n = 1:36
imageFileNames{n} = strcat(path,sprintf('image_%2d.jpg',n))
end
%------------------------------------------------------

Upvotes: 1

Views: 39

Answers (1)

Max
Max

Reputation: 4045

In this case, it is better to use the float-format-specifier to pad zeros:

num2str(1,'%02.f')

'01'

num2str(1,'%02.d')

'1'

num2str(1,'%2.d')

'1'

num2str(1,'%d')

'1'

So in your case:

sprintf('image_%02.f.jpg',n)

general advice As a general advice, you may want just to check the folder for all files of a certain pattern

path2dir = pwd; % path to working directory
pattern = 'image_*.jpg'
Lst = dir(fullfile(path2dir ,pattern));
for i = 1:length(Lst)
    path2file = fullfile(Lst(i).folder,Lst(i).name);
    % load the file and do something with it
end

Upvotes: 2

Related Questions