I have this cell array http://dl.dropbox.com/u/59868217/fileList.mat. Can I sort the array with respect to the number of characters that compose the file name? Cioè is possible obtain the array like this
'tot16/57583.raw'
'tot16/213587.raw'
'tot16/369571.raw'
'tot16/681550.raw'
'tot16/837534.raw'
'tot16/993529.raw'
'tot16/1149513.raw'
I obtain this list of file by this script function fileList = getAllFiles(dirName)
dirData = dir(dirName); %# Get the data for the current directory
dirIndex = [dirData.isdir]; %# Find the index for directories
fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files
if ~isempty(fileList)
fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files
fileList,'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories
%# that are not '.' or '..'
for iDir = find(validIndex) %# Loop over valid subdirectories
nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path
fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles
end
end

댓글 수: 1

Oleg Komarov
Oleg Komarov 2012년 5월 15일
'tot16/57583.raw'
'tot16/57582.raw'
Which one should come first?

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

 채택된 답변

Geoff
Geoff 2012년 5월 15일

0 개 추천

There's a couple of ways to do this... I assume you want the numbers in ascending order. This works, but relies on the stability of sort():
fileList = sort(fileList);
[~,I] = sort(cellfun(@length, fileList));
fileList = fileList(I);
The proper way to do it is:
n = cellfun(@(x) str2double(x{:}), regexp(fileList, '/(\d+)\.raw', 'tokens'));
[~, I] = sort(n);
fileList = fileList(I);
This converts the filenames to numbers, finds the sort order and rearranges the list accordingly.

댓글 수: 5

Walter Roberson
Walter Roberson 2012년 5월 16일
Differing numbers of directory names, different directory name lengths (it recurses on subdirectories). You can't just convert the filenames to numbers and fit within the user's request.
OTOH it is not clear whether the number of characters in the file name is intended to include the directory names or not.
Geoff
Geoff 2012년 5월 16일
I think I've provided enough for Geppo to adapt to his/her purposes, if the answer itself is not satisfactory. The solution works on the MAT file they provided, hence my suspicion that this question is actually about ordering the files based on the number in the filename.
Geppo Batt
Geppo Batt 2012년 5월 16일
fileList = sort(fileList);
[~,I] = sort(cellfun(@length, fileList));
fileList = fileList(I);
is work correctly. But the second way it's not work. return
Error using str2double (line 32)
Not enough input arguments.
Geppo Batt
Geppo Batt 2012년 5월 16일
fileList = sort(fileList);
[~,I] = sort(cellfun(@length, fileList));
fileList = fileList(I);
is work correctly. But the second way it's not work. return
Error using str2double (line 32)
Not enough input arguments.
Geoff
Geoff 2012년 5월 16일
That exact code works on the exact file list you provided. Did you type it in, or did you copy and paste it from my answer?

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

추가 답변 (1개)

per isakson
per isakson 2012년 5월 16일

0 개 추천

Try the FEX contribution sort_nat: Natural Order Sort

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

제품

질문:

2012년 5월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by