Ahsan Alii
Ahsan Alii

Reputation: 153

Sorting a cell array in matlab

I have an array of 81x1 cell of str. which looks like this, '1.png' '2.png' '100.png' '43.png' '20.png' '32.png' '98.png' and so on. I am trying to sort it using the sort function. sort(A) but it doesnt sort it. what should I do?

Upvotes: 1

Views: 47

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

Remove .png using regexprep or strrep, convert the remaining string to double and then use sort to get the sorted indices. Use these sorted indices to sort the cell A.

[~, ind] = sort(str2double(regexprep(A,'.png','')));
A = A(ind);

Upvotes: 2

Related Questions