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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by