Sorting a mixed text-numeric matrix (cell array) by one of its (numeric) columns
1 次查看(过去 30 天)
显示 更早的评论
I have an Excel spreadsheet whose rows I'd like to randomly shuffle and further use in Matlab. To that end, I'm reading the XLS into a cell array, generating random numbers into a column, then sorting by that column. This is proving to be problematic though, because of conflicting variable types. The code I used is:
[num, txt, M] = xlsread(fileName);
for row=2:row_N % generate random numbers in the D column
M(row,4) = num2cell(rand);
end
M = sortrows(M,4);
This leads to my matrix looking like this:
Because the D column contains numbers while there's text in the other columns, the sorting cannot be done (it says "Cell elements must be character arrays.").
I tried various conversions between numeric and cell variables, and also tried using 'readtable' instead of 'xlsread' - but there is always a problem, such that I cannot get this conflict solved.
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!