Get the name of picture

23 views (last 30 days)
Marta Ramos
Marta Ramos on 23 Jun 2018
Commented: Stephen23 on 12 Oct 2022
Hello, I have a folder with a lot of images inside and they are named like 1, 2, ...3, .... 54, .... , 1904. And i have this for to display the image
File = 'MyPath';
Ficheiro = fullfile(File, '*.jpg');
Image = dir(Ficheiro);
for k = 1:length(Image)
Image_name = Image(k).name;
Path = fullfile(File, Image_name);
%fprintf(1, 'Reading %s\n', Path);
Array_Image = imread(PathCompleto);
display(Array_image)
end
but if I have a display(Image_Name) I get "1.jpeg" and i want to have an array with ["1", "2", ..., "1904"]. Is there a way to split "1.jpeg" to "1" and insert all the image names into an array? Thank you
  1 Comment
Rik
Rik on 23 Jun 2018
The display function might not actually do what you mean.
The code below converts the file names without extension to a string array, although why you would want to use a string array here is a mystery to me.
filelist=dir(Ficheiro);
number_string_array=arrayfun(@getNameAndConvertToString,filelist);
function y=getNameAndConvertToString(x)
[~,filename,~]=fileparts(x.name);
y=string(filename);
end

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 23 Jun 2018
Edited: Stephen23 on 18 Apr 2021
It is easy to convert to a numeric array:
P = 'MyPath';
S = dir(fullfile(P,'*.jpg'));
N = {S.name};
[~,F] = cellfun(@fileparts,N,'uni',0);
V = str2double(F);
Note that one way to get the filenames into the correct numeric order would be to download my FEX submssion natsortfiles
S = natsortfiles(S); % alphanumeric sort by filename
or sort the vector V and use the second output:
[V,idx] = sort(V)
  3 Comments
Image Analyst
Image Analyst on 23 Jun 2018
Use the transpose operator, the apostrophe:
V = V'
Stephen23
Stephen23 on 12 Oct 2022
"how can I get 99 line and 1 column?"
V = V(:);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!