Sorting a table with strings and numbers

조회 수: 17 (최근 30일)
Aldrich To
Aldrich To 2015년 11월 10일
댓글: Aldrich To 2015년 11월 10일
Hi All,
So far, I am having great difficulty trying organize an imported excel spreadsheet as a table that contains both strings and numeric values. How do I go about sorting character strings while still retaining the numerical values?
Thanks!

채택된 답변

Image Analyst
Image Analyst 2015년 11월 10일
Use readtable() to read in the workbook
T = readtable(fullXlsFileName);
Then use sortrows() to sort the table based on the column with strings in it. Let's say that the column with strings is the first column, like in the example below. Then you'd just do:
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
% Create a table, T, as a container for the workspace variables.
T = table(LastName, Age,Height,Weight,BloodPressure)
% Sort rows by last name.
sortedTable = sortrows(T, 1)
In the command window, you'll see the unsorted table first, followed by the sorted table:
T =
LastName Age Height Weight BloodPressure
__________ ___ ______ ______ _____________
'Smith' 38 71 176 124 93
'Johnson' 43 69 163 109 77
'Williams' 38 64 131 125 83
'Jones' 40 67 133 117 75
'Brown' 49 64 119 122 80
sortedTable =
LastName Age Height Weight BloodPressure
__________ ___ ______ ______ _____________
'Brown' 49 64 119 122 80
'Johnson' 43 69 163 109 77
'Jones' 40 67 133 117 75
'Smith' 38 71 176 124 93
'Williams' 38 64 131 125 83
  댓글 수: 1
Aldrich To
Aldrich To 2015년 11월 10일
Thanks Image Analyst! Resolved my problem!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by