How can I create a fullpath from images of different subfolders?

I have images with repitative names in diffrent folders, I want to read them all and use imread. The problem is when I use dir it creates diffrent columns but when i want to create a full path the result is 0.
*******************
path = 'C/..../mainfolder/'];
country= ....
files=dir([datapath 'run','*',country,'.png'])
for k= 1:length(files)
t= strcat(files(k).folder,files(k).name)
end

 Accepted Answer

Do not use path as a variable name (you shadow the inbuilt function of that name).
Do not concatenate strings to build paths.
Use fullfile instead, e.g.:
pattern = sprintf('run*%s.png',country)
files = dir(fullfile(datapath,pattern))
and
fullfile(files(k).folder,files(k).name)
Note that
C/
is unlikely to resolve to anything useful on any OS.

5 Comments

The subfolders containing pictures are named as years. I need to be flexible in choosing the year and listing the.
%activity= 'run' or 'dart'
year1 = 1980
year2= 2010
for num= year1:year2
switch activity
case 'Indai'
path = ['C\folder\competition\run','\',num2str(num),'\']; % get images with this name from all subfolders
if strcmp(age,'rd')
ag = 'old';
list_images= dir([datapath 'run_',num2str(d),'*_',country,ag,'.png'])
elseif strcmp(age,'rs') %German
ag = '';
list_images=dir([path 'run_',num2str(d),'*_',country,ag,'.png']);% get images with this name from all subfolders
end
ti = 1;
case 'Africa'
path = ['C\folder\competition\dart','\',num2str(num),'\'];
list_images=dir([datapath 'dart',country,'_',num2str(d),'*.png'])% get images with this name from all subfolders
ti = 2;
end
for i=1:length(images)
filename = strcat(images(i).folder,'/',images(i).name);% Concatenate strings horizontally to create a full path
I= imread(filename);% fo further process of showing image a series
end
it can not give me in "list_images" all the images of the requested names for all the years.
You should avoid using path as a variable name, because it conflicts with the path() function.
filedir = fullfile('C:', 'folder', 'competition', 'run', sprintf('%d',num));
if strcmp(age, 'rd')
ag = 'old';
list_images = dir(filedir, sprintf('run_%d*_%s%s.png', d, country, ag))
elseif strcmp(age, 'rs')
ag = '';
list_images = dir(filedir, sprintf('run_%d*_%s%s.png', d, country, ag))
end
You were using C\ instead of C:
You were defining path but using datapath instead.
It is cleaner to use fullfile() rather than string concatenation.
Walter Roberson also forgot to use fullfile (note that dir only accepts one input argument):
filedir = fullfile('C:', 'folder', 'competition', 'run', sprintf('%d',num));
if strcmp(age,'rd')
ag = 'old';
elseif strcmp(age,'rs')
ag = '';
else
% ? what to do ?
end
list_images = dir(fullfile(filedir, sprintf('run_%d*_%s%s.png', d, country, ag)));

Sign in to comment.

More Answers (1)

Its not quite clear from the snippet of code and description you give but I think your problem is that you are not constructing the path to your files correctly. Note that if the path is incorrect and no files will be found then executing
files=dir([datapath 'run','*',country,'.png'])
will return
files =
0×1 empty struct array with fields:
name
folder
date
bytes
isdir
datenum
Which I think maybe is what you mean by the "result is 0"

9 Comments

Yes I got that problem. In other methods again the path was problem.
To help you further I would need to better understand the details of what you are trying to do, what you have tried, and what problem you encountered
Please 1) Attach a screenshot from Windows File Explorer (or equivalent for your OS) showing exactly what your directory structure looks like. 2) Attach, or paste in (using the Code formatting button on the Answers toolbar) the code that you are executing and 3) Copy and Paste the full error message you get from MATLAB
I comented on top, thank you for your help .
I looked at your comments and I'm still not really clear what your exact directory structure is. Anyhow, a few comments.
First, I would recommend not using "path" as a variable name , path is very important MATLAB function that returns the current search path. If you use it as a local variable name, then that definition takes precedence and path command will no longer work.
That aside, I also noticed that while you assign the variable path, you don't alway use the value you assign to it. So for example
path = ['C\folder\competition\run','\',num2str(num),'\']; % get images with this name from all subfolders
if strcmp(age,'rd')
ag = 'old';
list_images= dir([datapath 'run_',num2str(d),'*_',country,ag,'.png'])
elseif strcmp(age,'rs') %German
ag = '';
list_images=dir([path 'run_',num2str(d),'*_',country,ag,'.png']);% get images with this name from all subfolder
you assign the variable path, but then you don't use it if strcmp(age,'rd'), instead you use datapath
I'm not sure where datapath is assigned, so it is hard to know if it is assigned to the path you want.
Also I don't understand your final loop
for i=1:length(images)
filename = strcat(images(i).folder,'/',images(i).name);% Concatenate strings horizontally to create a full path
I= imread(filename);% fo further process of showing image a series
end
I don't see where you have ever assigned the variable images, that you reference "images(i).folder". It seems like you should be using list_images instead.
In general, I would recommend stepping through your code with the debug tool and examining the values that you are assigning to the paths to see if they make sense.
Thank you for your time, yes I had some mistake to put the right code here.
The problem is with the first for-loop, instead of listing the pictures from different years(subfolders), it just gives the last year 2010. not any other previous years.
%activity= 'run' or 'dart'
year1 = 1980
year2= 2010
for num= year1:year2
switch activity
case 'Indai'
% dpath= ['C\folder\competition\run\1980\run_19800101_India_old.png']
% images with other month and date in 1980 folder are here.
% dpath= ['C\folder\competition\run\1981\run_19810401_India_old.png']
% the next folder with a list of images with other month and date in 1981 .
% this will be continued till the last folder 2010
dpath= ['C\folder\competition\run','\']
dpath = [dpath,num2str(num),'\']; % get images with this name from all subfolders
if strcmp(age,'rd')
ag = 'old';
list_images= dir([dpath 'run_',num2str(d),'*_',country,ag,'.png'])
elseif strcmp(age,'rs') %German
ag = '';
% dpath= ['C\folder\competition\run\1980\run_19800101_India.png'] more than 100 folder with date changing
list_images=dir([path 'run_',num2str(d),'*_',country,ag,'.png']);% get images with this name from all subfolders
end
ti = 1;
case 'Africa'
% dpath= ['C\folder\competition\dart\1980\run_19800102_Africa.png'] more than 100 folder with date changing
% in between other images
% dpath= ['C\folder\competition\dart\2002\run_20020102_Africa.png'] more than 100 folder with date changing
dpath = ['C\folder\competition\dart','\',num2str(num),'\'];
images=dir([dpath 'dart',country,'_',num2str(d),'*.png'])% get images with this name from all subfolders
ti = 2;
end
for i=1:length(images)
filename = strcat(images(i).folder,'/',images(i).name);% Concatenate strings horizontally to create a full path
I= imread(filename);% fo further process of showing image a series
end
I'm not clear, have you found a solution to your problem, or are you still having a problem?
no not really I tried in any different possible cases ?!
I understand from your comment that you still have not solved your problem. If you would like further help, please try to make a self contained example (a short script along with any necessary data files for it to run) that demonstrates the problem you are encountering. Also copy and paste the full text of the error messages you encounter when you try to run your example.
Comment posted as flag by @FSh:
helpful inforamtion

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Asked:

FSh
on 24 Jul 2019

Commented:

Rik
on 25 Jan 2021

Community Treasure Hunt

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

Start Hunting!