MATLAB: How to access names of pictures in a file

arraysfilefunctionMATLABwhile loop

The name of the images have important information that I need to store in different variables for further processing.
For example the name of the image is: gh_368053_209864_19
And there are 8 images with different numbers. I need to access 368053 and store it in x separately and store 209864 in y. And I want to do this for all the images in the file in a loop. Store x and y in two different arrays.
I will also apply other functions to the accessed images!
How can this be done only with names of the images?
clear all
clc;
location = 'E:\ATR\*.JPG'; % folder in which your template images exists
img= imread('E:\ATR\gh_368053_209864_19.JPG');
ds = imageDatastore(location); % Creates a datastore for all images in your folder
% fileNames={a.name};
while hasdata(ds)
temp = read(ds); % read image from datastore
% Image_name = temp.name;
% figure, imshow(temp)
end

Best Answer

if all the image names follow the pattern: gh_someNumber1_someNumber2_someNumber3
and you need to store someNumber1 and someNumber2 in separate variables:
% example array of file names:
example_img_names = {...
'E:\ATR\gh_368053_209864_19.JPG',...
'E:\ATR\gh_542191_032861_19.JPG',...
'E:\ATR\gh_541666_417980_19.JPG'};
% make empty cell array to hold all the x numbers (all someNumber1's).
% initialize it with size of 1 x number of image names

all_x_numbers = cell(1,length(example_img_names));
% make empty cell array to hold all the y numbers (all someNumber2's).
% initialize it with size of 1 x number of image names
all_y_numbers = cell(1,length(example_img_names));
% for loop to go through all example_img_name
for i = 1:length(example_img_names)
% get image name at current index
curr_img_name = example_img_names{i};
% break up filename into parts
[filepath,name,ext] = fileparts(curr_img_name);
% split filename by '_' character
array_of_splits = strsplit(name, '_');
% second element in split array will be someNumber1
all_x_numbers{i} = array_of_splits{2};
% third element in split array will be someNumber2
all_y_numbers{i} = array_of_splits{3};
end
disp("x numbers:")
disp(all_x_numbers)
disp("y numbers:")
disp(all_y_numbers)