Borrar filtros
Borrar filtros

Create a list of images from subfolders with specific names using dir , strcat!

3 visualizaciones (últimos 30 días)
There should be two seperate for-loop but in the first loop it overwright and just I can get the last year images. I also tried fullfile but I got problem with the imread in the second loop because of the path name. I want to create a list of all images with special names as what assgined below among the total number located in diffrent subfolder. But It just gives the last years.
%activity= 'run' or 'dart'
year1 = 1980
year2= 2010
for num= year1:year2
switch activity
case 'Indai'
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 subfolders
end
ti = 1;
case 'Africa'
path = ['C\folder\competition\dart','\',num2str(num),'\'];
list_images=dir([datapath 'dart',country,'_',num2str(d),'*.png'])% get images with this name from all subfolders
ti = 2;
end
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

Respuesta aceptada

Guillaume
Guillaume el 26 de Jul. de 2019
Very confusing code and confusing question.
But It just gives the last years.
Well, yes of course, in the loop you overwrite list_images at each step. So when the loop finishes, you're left with just the value you've written last.
You could rewrite your loop as such:
year1 = 1980
year2 = 2010
years = year1:year2;
list_images = cell(size(years));
for yearindex = 1:numel(years)
num = years(yearindex); %num is an extremely poor variable name!
%....
list_images{yearindex} = ...
end
But the whole thing should be rewritten to extract the switch and the if...elseif out of the loop, and using fullfile indeed instead of char vector concatenation. fullfile is always more reliable and if you've got problem (what problem? always state the full text of any error you get), then it's because you've made a mistake, not because it doesn't work.
  2 comentarios
vania todorova
vania todorova el 23 de Jul. de 2021
newbie here. why does it have to be specifically a cell array ? is it something that works with images better? i m mostly a python user and i would just use a list but trying to figure out how to separate image ds into groups of 5 images easiest . thanks!
FSh
FSh el 24 de Jul. de 2021
In my experince, Guillaume might know better!
Surely there can be multiple solutions to a problem and if that works for you, there should be no issue.
In my case I had multiple folders (..., 2018, 2019, 2020, ...) in my main directory and I wanted to run the process for only 2 different years each time. So a simple dir could not help in this case!!!!
But I also experienced that if there are inconsistencies in array size, cell is the best solution.
So I used this;
list_images2 = struct('name',{},'folder',{},'date',{},'bytes',{},'isdir',{},'datenum',{});
list_images = struct('name',{},'folder',{},'date',{},'bytes',{},'isdir',{},'datenum',{});
for yearindex = Y1:Y2
datapath = [main_path,'sample' ,'/',num2str(yearindex),'/'];
list_images = dir([datapath 'image_',num2str(yearindex),'*_*.tif']);
list_images= list_images(:,1);
list_images2 = [list_images2;list_images];
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by