How can I create a fullpath from images of different subfolders?
Show older comments
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
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
FSh
on 24 Jul 2019
Jon
on 26 Jul 2019
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
FSh
on 26 Jul 2019
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.
FSh
on 26 Jul 2019
Jon
on 30 Jul 2019
I'm not clear, have you found a solution to your problem, or are you still having a problem?
FSh
on 9 Aug 2019
Jon
on 9 Aug 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.
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!