This is a bit of a hacker version, but it works roughly:
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
Edric source share