matlab not reading images from folder in correct sequence

3 ビュー (過去 30 日間)
mukesh bisht
mukesh bisht 2019 年 11 月 20 日
編集済み: Erivelton Gualter 2019 年 11 月 20 日
Hi, i have stored the images in a folder by name ex. 1.jpg 2.jpg etc. When i am running the code the matlab doesn't read the images in that sequence. Please help
directory = 'C:\Users\Documents\MATLAB\photos';
files = dir([directory '/*.jpg']);
for i = 1:length(files)
image{i} = imread([directory '/' files(i).name]);

回答 (1 件)

Erivelton Gualter
Erivelton Gualter 2019 年 11 月 20 日
編集済み: Erivelton Gualter 2019 年 11 月 20 日
It had happened to me before. The problem is the files name is sorted in according to the strings.
Let's say you have the following images in your folder:
  • 1.jpg
  • 2.jpg
  • ...
  • 10.jpg
  • 11.jpg
If you run the following code:
for i=1: length(files)
files(i).name % Shows the name of the files
end
The output will be:
ans =
'10.png'
ans =
'11.png'
ans =
'1.png'
ans =
'2.png'
...
If you rename your files as in the following, the files will be sorted correctly:
  • 01.jpg
  • 02.jpg
  • ...
  • 10.jpg
  • 11.jpg

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by