How can I create cell arrays that are in sequential order?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I've written this code to iterate through a series of .jpg images in a folder in order to create greyscale images of each picture. However, the folder creates cell arrays that aren't in sequential order as in (1, 2, 3, 4...). Instead, it goes 136, 137, 138, 139, 14, 140, 141, 142, 143... and so forth. How can I create cell arrays that are in sequential order?
0 Commenti
Risposte (2)
the cyclist
il 16 Mag 2018
Modificato: the cyclist
il 16 Mag 2018
For example, you could ensure that the number 1 is expressed as 001, ensuring the ordering.
sprintf('%03d',1)
ans =
'001'
0 Commenti
Stephen23
il 17 Mag 2018
Modificato: Stephen23
il 7 Set 2019
Two simple solutions:
- add sufficient leading zeros to all of the numbers (then a character sort will give the correct order). The tricky thing here is knowing in advance how many is sufficient... this is a fragile solution because in the future a user might easily define more images but not change how many leading zeros there are.
- download my FEX submission natsortfiles, which was written to solve exactly this problem. Use it simply like the examples in the help, the HTML documentation and the FEX description show. In your case you will want something like this:
D = uigetdir(...);
S = dir(fullfile(D,'*.jpg'));
C = natsortfiles({S.name});
N = numel(C);
I = cell(1,N);
for k = 1:N
F = fullfile(D,C{k});
I{k} = imread(F);
...
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!