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

12 ビュー (過去 30 日間)
FSh
FSh 2019 年 7 月 24 日
コメント済み: Rik 2021 年 1 月 25 日
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

採用された回答

Stephen23
Stephen23 2019 年 7 月 24 日
編集済み: Stephen23 2019 年 7 月 28 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 7 月 26 日
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 2019 年 7 月 28 日
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)));

サインインしてコメントする。

その他の回答 (1 件)

Jon
Jon 2019 年 7 月 24 日
編集済み: Jon 2019 年 7 月 24 日
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 件のコメント
Jon
Jon 2019 年 8 月 9 日
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 2021 年 1 月 25 日
Comment posted as flag by @FSh:
helpful inforamtion

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by