reading multiple image from multiple folders in matlab - matlab

I have a folder(Enroll) which contain 100 or more sub folders and each of them contain one image. I want to read this image and do some processing on this image. I have problem with how to read them from the different folders ?
note *
( the sub folders name is number like : 1, 2,.. " this number arrived from user " )
(the image name is number but different and not sequential like : 433535.bmp , 126554,bmp ,...)
foldername=1; // name of the sub folder arrived from user
d4= dir('C:\Users\Sarah\Desktop\Log\Log\Enroll\',foldername,'\*.bmp');// here problem when i put foldername variable
foldername2=d4(1).name;
w=imread(fullfile('C:\Users\Sarah\Desktop\Log\Log\Enroll\',foldername,'\*.bmp', foldername2));
help me please :(

foldername is not a string. therefore, you need to make it a string. I believe that what you want is
d4= dir(['C:\Users\Sarah\Desktop\Log\Log\Enroll\' num2str(foldername) '\*.bmp']);
Note:
1- you need to convert from number to string whatever number you have. If foldername is a string, then num2str is not needed.
2- You need to concatenate arrays, it doesn't happen automatically. therefore you need to ad the brackets [].

Related

How to remove last 2 positions in split and get remaining first value.?

Let's say string is a variable file name like few examples below:
file1_name_cr_001.csv
file2_name1_name2.nn.123.456_updt_000.csv
filename_2012.444.1234_utc_del_004.csv
The length of last 8 string values will always remain fixed i.e. (_001.csv,_000.csv,_004.csv). We need to only extract values = cr, updt, del
How can we get the value as single value before _cr,_updt,_del.?
any suggetions.?
output should get like this:
file1_name/cr/001
file2_name1_name2.nn.123.456/updt/000
filename_2012.444.1234_utc/del/004
I have reproduced the above and got the below results.
First, I took a sample file name in set variable.
Then, I got the string from start to length-8.
#substring(variables('sample'),0,sub(length(variables('sample')),8))
For end folder:
#replace(split(substring(variables('sample'),sub(length(variables('sample')),8), 8),'.')[0],'_','')
For Start folder:
#substring(variables('before_8'), 0, lastIndexOf(variables('before_8'), '_'))
For middle folder:
#split(variables('before_8'), '_')[sub(length(split(variables('before_8'), '_')), 1)]
Result folder structure:
#concat(variables('start'),'/',variables('middle'),'/',variables('end'))
Result:
Give this variable in copy activity source folder path and it will generate the folder structure for you.
For multiple file names, first store all file names in an array then use a ForEach and inside ForEach do the same operations as above.

How to list down mat files with a specific prefix name?

I have a folder of images saved as .mat files files with the following names:
image-001-001.mat,image-001-002.mat,......., image-001-102.mat, image-002-001.mat,image-002-002.mat, ....,image-002-090.mat, etc.
I want to group the file names for each prefix. For example, list down all files that starts with image-001- prefix and list all images with image-002- and etc. for all files in the folder. I need the images of each group separately and do some processing on them.
Could someone please give some hints how can I do it?
Thanks in advance
See the documentation for dir, specifically the mention of wildcards.
You can get a list of .mat files which start with image-001 using
files_001 = dir('C:\myfolder\image-001*.mat');
% or if it's in the current directory then simply
% files_001 = dir('image-001*.mat');
To loop over several prefixes, you could use
prefixes = {'image-001', 'image-002', 'image-003'};
files = cell(numel(prefixes), 1);
for p = 1:numel(prefixes)
files{p} = dir([prefixes{p}, '*.mat']);
end
Aside:
If your prefixes really are that similar / ordered, there are many ways (e.g. using strcat) to quickly make the prefixes cell array.
YOu can pick all the images with start with image-001-xxx.mat as below:
files1 = dir('image-001*') ;

Get a list of all subdirectories in Matlab

I'm trying get an absolute path of all subfolders in project_dirs.
project_dirs='D:\MPhil\Model_Building\Models\TGFB\Vilar2006\SBML_sh_ver\vilar2006_SBSH_test7\Python_project3_IQM_project';
all_project_dirs=dir(project_dirs)
for i=all_project_dirs,
full_dir=fullfile(project_dirs,i.name)
The above code gives a single string of all the subfolder directories concatenated together. How do I modify my code to get a cell array of these absolute paths?
There's a function for that: genpath(). It will give you all directories recursively in a string, split by :. Use strsplit() to parse the result.
You can do this:
all_project_dirs = {all_project_dirs([all_project_dirs.isdir]).name};
How it works:
This selects, among the elements of all_project_dirs, those that are directories;
From them it gets the name field;
The values of that field are contatenated into a cell array.
You may want to remove the first two directory names, which are always '.' and '..':
all_project_dirs = all_project_dirs(3:end);
To obtain full paths, you can use strcat:
all_project_dirs = strcat(project_dirs, filesep, all_project_dirs);
or, as suggested by Jørgen, use fullfile:
all_project_dirs = fullfile(project_dirs, all_project_dirs);

Store user input as wildcard

I am having some trouble with a data processing function in MATLAB. The function takes the name of the file to be processed as an input, finds the desired files, and reads in the data.
However, several of the desired files are variants, such as Data_00.dat, Data.dat, or Data_1_March.dat. Within my function, I would like to search for all files containing Data and condense them into one usable file for processing.
To solve this, I would like desiredfile to be converted into a wildcard.
Here is the statement I would like to use.
selectedfiles = dir *desiredfile*.dat % Search for file names containing desiredfile
This returns all files containing the variable name desiredfile, rather than the user input.
The only solution that I can think of is writing a separate function that manually condenses all the variants into one file before my function is run, but I am trying to keep the number of files used down and would like to avoid this.
You could concatenate strings for that. Considering desiredFile as a variable.
desiredFile = input('Files: ');
selectedfiles = dir(['*' desiredfile '*.dat']) % Search for file names containing desiredfile
Enclosing strings between square brackets [string1 string2 ... stringN]concatenates them. Matlab's dir function receives a string.
I believe you can achieve that using the dir command.
dataSets = dir('/path/to/dir/containing/Data*.dat');
dataSets = {dataSets.name};
Now simply loop over them, more information here.
To quote the matlab help:
dir lists the files and folders in the MATLAB® current folder. Results appear in the order returned by the operating system.
dir name lists the files and folders that match the string name. When name is a folder, dir lists the contents of the folder. Specify name using absolute or relative path names. You can use wildcards (*).

looping and ordering extracted data in MatLab

I have hundreds of data files, named file001~file400 for example, and should pick a numeric value from each. I know how to pick each number, but, since there are too many files, I need to loop the command and order all extracted numbers with regard to the number of corresponding file. I appreciate any help.
The problem is to loop over all files. This can be done in the following way:
for i = 1:400
filename = sprintf('file%03d',i);
// do the number picking, etc. using the filename.
end
EDIT: Per request, for filenames FT00100 to FT05320, we we make two small changes, one in the loop range and one in the first parameter to sprintf:
for i = 100:5320
filename = sprintf('FT%05d',i);
// do the number picking, etc. using the filename.
end