Get the name of picture
23 views (last 30 days)
Show older comments
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
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
Accepted Answer
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)
More Answers (0)
See Also
Categories
Find more on Low-Level File I/O 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!