problem with sorting a structure

조회 수: 2 (최근 30일)
Efstathios Kontolatis
Efstathios Kontolatis 2016년 9월 14일
답변: KSSV 2016년 9월 14일
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?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 9월 14일
images=dir('file*.tif');
n = {images.name}';
[~,ii] = sort(str2double(regexp(n,'(?<=file)\d*(?=\.txt)','match','once')));
images = images(ii);
  댓글 수: 1
Efstathios Kontolatis
Efstathios Kontolatis 2016년 9월 14일
Thank you very much that worked fine for me

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KSSV
KSSV 2016년 9월 14일
  댓글 수: 1
Efstathios Kontolatis
Efstathios Kontolatis 2016년 9월 14일
Thank you for your answer. However, this links use cell arrays. I don't want to convert my struct to a cell. I want to sort the struct in a way that I will have the same struct after executing but with the names sorted.

댓글을 달려면 로그인하십시오.


KSSV
KSSV 2016년 9월 14일
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) ;

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by