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

17 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Stephen23
Stephen23 il 24 Lug 2019
Modificato: Stephen23 il 28 Lug 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 Commenti
Walter Roberson
Walter Roberson il 26 Lug 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 il 28 Lug 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)));

Accedi per commentare.

Più risposte (1)

Jon
Jon il 24 Lug 2019
Modificato: Jon il 24 Lug 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 Commenti
Jon
Jon il 9 Ago 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.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by