matlab 按文件名排序,在MATLAB中对字符串进行排序,如Windows 7,对资源管理器中的文件名进行排序(尊重数字中间字符串)…

  • Post author:
  • Post category:其他


这是一个hacky版本,但它大致有效:

function x = sortit(x)

% get a sortable version of each element of x

hacked_x = cellfun( @iSortVersion, x, ‘UniformOutput’, false );

% sort those, discard the sorted output

[~, idx] = sort( hacked_x );

% re-order input by sorted order.

x = x(idx);

end

% convert a string with embedded numbers into a sortable string

function hack = iSortVersion( in )

pieces = regexp( in, ‘(\d+|[^\d]+)’, ‘match’ );

pieces = cellfun( @iZeroPadNumbers, pieces, ‘UniformOutput’, false );

hack = [ pieces{:} ];

end

% if a string containing a number, pad with lots of zeros

function nhack = iZeroPadNumbers( in )

val = str2double(in);

if isnan(val)

nhack = in;

else

nhack = sprintf( ‘%030d’, val );

end

end