Hi
How can I sort a table-type array in ascending order
For example
First mode
To:
TNX :)

1 件のコメント

Stephen23
Stephen23 2020 年 11 月 12 日
@Shahar ben ezra: you should transpose the way your table is arranged, otherwise it will be a nightmare to work with (both in Excel and MATLAB).

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

 採用された回答

Steve Eddins
Steve Eddins 2020 年 11 月 12 日
編集済み: Steve Eddins 2020 年 11 月 12 日

1 投票

The answer would be relatively straightforward, as well as much more efficient, if you would orient your table the other way. With data such as yours, table is really designed to be used this way:
>> Name = ["roni" ; "tim" ; "jon" ; "lie" ; "kim"];
>> TestScore = [90 ; 45 ; 67 ; 84 ; 32];
>> T = table(Name,TestScore)
T =
5×2 table
Name TestScore
______ _________
"roni" 90
"tim" 45
"jon" 67
"lie" 84
"kim" 32
Then you could sort the table using sortrows:
>> T2 = sortrows(T,"TestScore")
T2 =
5×2 table
Name TestScore
______ _________
"kim" 32
"tim" 45
"jon" 67
"lie" 84
"roni" 90

5 件のコメント

Shahar ben ezra
Shahar ben ezra 2020 年 11 月 12 日
thank you for the quick reply
When you read the table from Excel
How do you turn ferst Vector into Char?
Steve Eddins
Steve Eddins 2020 年 11 月 12 日
Excel also generally functions better when data is stored in columns rather than rows. Are you able to make this change to your spreadsheet? If so, then you can use readtable:
T = readtable(filename,"TextType","string");
Shahar ben ezra
Shahar ben ezra 2020 年 11 月 12 日
This is an example of something much bigger
I can not change the columns and rows
Do you know why it does not work?
Steve Eddins
Steve Eddins 2020 年 11 月 12 日
The convention of storing tabular data column-wise is almost universal today. Excel itself follows it. A couple of months ago, there were some articles in the press about a UK coronavirus dataset that was corrupted because the data was stored row-wise in an Excel spreadsheet.
You can still get it done with your spreadsheet files, though. It just requires a few extra steps. One way is to read the Excel file into a cell array and then manipulate the cell array to get the desired MATLAB table, like this:
>> C = readcell('wrong-way.xlsx')
C =
2×6 cell array
Columns 1 through 5
{'Name' } {'roni'} {'tim'} {'jon'} {'lie'}
{'Test score'} {[ 90]} {[ 45]} {[ 67]} {[ 84]}
Column 6
{'kim'}
{[ 32]}
>> Name = string(C(1,2:end))'
Name =
5×1 string array
"roni"
"tim"
"jon"
"lie"
"kim"
>> TestScore = cell2mat(C(2,2:end))'
TestScore =
90
45
67
84
32
>> T = table(Name,TestScore)
T =
5×2 table
Name TestScore
______ _________
"roni" 90
"tim" 45
"jon" 67
"lie" 84
"kim" 32
>> T2 = sortrows(T,"TestScore")
T2 =
5×2 table
Name TestScore
______ _________
"kim" 32
"tim" 45
"jon" 67
"lie" 84
"roni" 90
Shahar ben ezra
Shahar ben ezra 2020 年 11 月 12 日
thanks for your help!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import from MATLAB についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by