How to sort data once it is read into matlab

11 ビュー (過去 30 日間)
Aaron
Aaron 2013 年 10 月 18 日
コメント済み: Cedric 2013 年 10 月 18 日
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
Azzi Abdelmalek 2013 年 10 月 18 日
Type
whos data
whos matrix_data

サインインしてコメントする。

採用された回答

Cedric
Cedric 2013 年 10 月 18 日
編集済み: Cedric 2013 年 10 月 18 日
The first column of data is a cell array of strings, whereas columns 2 and 3 are numeric arrays. You cannot concatenate them without performing first a conversion. You can also convert numeric arrays to cell arrays and have both strings and numbers in a large cell array. E.g.
matrix_data = [data{1}, num2cell(data{2}), num2cell(data{3})] ;
or
matrix_data = [data{1}, num2cell([data{2:3}])] ;
that you can then sort:
>> sort_data = sortrows(matrix_data, [1,3])
sort_data =
'A88888888888888888880011' [-65] [ 1]
'A88888888888888888880011' [-52] [ 2]
'A88888888888888888880011' [-64] [ 3]
'A88888888888888888880011' [-52] [ 4]
'A88888888888888888880011' [-57] [ 5]
'A88888888888888888880011' [-65] [ 6]
'A88888888888888888880011' [-57] [ 7]
'A88888888888888888880011' [-55] [ 8]
'A88888888888888888880012' [-66] [ 9]
'A88888888888888888880012' [-52] [10]
'A88888888888888888880012' [-68] [11]
'A88888888888888888880012' [-64] [12]
'A88888888888888888880012' [-44] [13]
'A88888888888888888880012' [-64] [14]
'A88888888888888888880012' [-40] [15]
'A88888888888888888880012' [-51] [16]
'A88888888888888888880013' [-55] [17]
'A88888888888888888880013' [-57] [18]
'A88888888888888888880013' [-49] [19]
'A88888888888888888880013' [-47] [20]
'A88888888888888888880013' [-64] [21]
'A88888888888888888880013' [-60] [22]
'A88888888888888888880013' [-67] [23]
'A88888888888888888880013' [-61] [24]

その他の回答 (1 件)

Vivek Selvam
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 件のコメント
Cedric
Cedric 2013 年 10 月 18 日
編集済み: Cedric 2013 年 10 月 18 日
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'

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by