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

12 vues (au cours des 30 derniers jours)
FSh
FSh le 24 Juil 2019
Commenté : Rik le 25 Jan 2021
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

Réponse acceptée

Stephen23
Stephen23 le 24 Juil 2019
Modifié(e) : Stephen23 le 28 Juil 2019
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 commentaires
Walter Roberson
Walter Roberson le 26 Juil 2019
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.
Stephen23
Stephen23 le 28 Juil 2019
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)));

Connectez-vous pour commenter.

Plus de réponses (1)

Jon
Jon le 24 Juil 2019
Modifié(e) : Jon le 24 Juil 2019
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 commentaires
Jon
Jon le 9 Août 2019
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.
Rik
Rik le 25 Jan 2021
Comment posted as flag by @FSh:
helpful inforamtion

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by