problem with sorting a structure
1 view (last 30 days)
Show older comments
Efstathios Kontolatis
on 14 Sep 2016
I have a structure created by the dir command so I have something like that
images=dir('file*.tif')
The problem is that the struct created has its names that go like this
file1.tif
file10.tif
file2.tif
file20.tif
I want to sort all the contents of the struct so that it'll go like
file1.txt
file2.txt
file3.txt
...
file10.txt
But I want to do it without using cells. Just to sort the structure. Is there a way to do so?
0 Comments
Accepted Answer
Andrei Bobrov
on 14 Sep 2016
images=dir('file*.tif');
n = {images.name}';
[~,ii] = sort(str2double(regexp(n,'(?<=file)\d*(?=\.txt)','match','once')));
images = images(ii);
More Answers (2)
KSSV
on 14 Sep 2016
Try this once and let me know:
clc; clear all ;
f=dir('file*.txt') ; % get txt files in the folder
nfiles = length(f) ; % total number of files
iwant = zeros(nfiles,1) ; % numbers at the end of files
for i = 1:nfiles
B = regexp(f(i).name,'\d*','Match'); % get the number at the end of file
iwant(i) = str2num(B{1}) ;
end
%%Sort the numbers iwant
[val,idx] = sort(iwant) ; % sorting the numbers
sort_f = f(idx) ;
0 Comments
See Also
Categories
Find more on Structures 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!