Sorting a table with strings and numbers
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aldrich To
el 10 de Nov. de 2015
Comentada: Aldrich To
el 10 de Nov. de 2015
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!
0 comentarios
Respuesta aceptada
Image Analyst
el 10 de Nov. de 2015
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!