How to sort data once it is read into matlab
显示 更早的评论
I am trying to sort the attached file first by column 1, then by column 3. I have tried the following code:
fid = fopen(filename);
data = textscan(fid, '%s %f %f');
fclose(fid);
matrix_data = [data{:}];
sort_data = sortrows(matrix_data, [1,3]);
This tells me that CAT arguments dimensions are not consistent.
Could someone tell me what is wrong with this code?
Thanks.
1 个评论
Azzi Abdelmalek
2013-10-18
Type
whos data
whos matrix_data
采纳的回答
更多回答(1 个)
Vivek Selvam
2013-10-18
This should solve your problem.
data = textscan(fid, '%s %s %s');
instead of
data = textscan(fid, '%s %f %f');
1 个评论
Careful if you are comparing numbers which can change sign though, as this is sorting strings only:
>> sortrows( {'-88'; '-8'; '9'} )
ans =
'-8'
'-88'
'9'
类别
在 帮助中心 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!