Resorting a list generated by subdirectories

조회 수: 10 (최근 30일)
Jason
Jason 2015년 11월 27일
댓글: Jason 2015년 11월 27일
Hi. I am trying to read in images from a set of sub directories. I first need to list all directories off my main directories (called newpath). I do this by:
cd(newpath)
p=dir *c*
This generates a list of subdirectoires. Becasue I want to plot the mean of the directories in order, I need to resort the list. At the moment C1.1 is first, then C10.1 etc. I need C1.1, C2.1, C3.1 etc.
Any ideas please?
C1.1 C113.1 C128.1 C142.1 C157.1 C171.1 C186.1 C20.1 C214.1 C229.1 C243.1 C258.1 C272.1 C287.1 C300.1 C43.1 C58.1 C72.1 C87.1
C10.1 C114.1 C129.1 C143.1 C158.1 C172.1 C187.1 C200.1 C215.1 C23.1 C244.1 C259.1 C273.1 C288.1 C301.1 C44.1 C59.1 C73.1 C88.1
C100.1 C115.1 C13.1 C144.1 C159.1 C173.1 C188.1 C201.1 C216.1 C230.1 C245.1 C26.1 C274.1 C289.1 C302.1 C45.1 C6.1 C74.1 C89.1
C101.1 C116.1 C130.1 C145.1 C16.1 C174.1 C189.1 C202.1 C217.1 C231.1 C246.1 C260.1 C275.1 C29.1 C31.1 C46.1 C60.1 C75.1 C9.1

채택된 답변

Thorsten
Thorsten 2015년 11월 27일
편집: Thorsten 2015년 11월 27일
BTW, you don't need to change the directory. Just use
d = dir(fullfile(newpath, '*c*'));

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 11월 27일
Stephen's submission is probably fine, maybe better. The one before it, from Doug Schwarz, is http://blogs.mathworks.com/pick/2006/09/11/file1-file10-file2-sorting-problem-solved/
By the way Jason, you might also want to look at genpath(), maybe not for this case, but it's good to know about, and is needed in some cases, such as if you want to recursively go into all subdirectories looking for more subdirectories, like my attached demo does.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by