How to sort an array by row based on the the first column

1 view (last 30 days)
I am trying to organize data that I am trying to work with. It is in a cell array with 9 columns.
I have also defined a structure for this array and the different variables. Each row begins with a string (the name of a certain station), and is followed by several numbers that the station measured. I want to be able to organize the data by station so that I can work with data from each station by itself.
I am new to MATLAB and not all too familiar with many commands, so any help would be much appreciated.

Accepted Answer

Image Analyst
Image Analyst on 20 Oct 2015
Try this:
% Define sample data.
ca = {'row1', 1,2,3; 'row2', 14,15,16; 'row3', 7,8,9}
% Convert columns 2 through the end to a regular numerical matrix.
measurements = cell2mat(ca(:, 2:end))
% Sort based on ascending first column
sortedMeasurements = sortrows(measurements, 1)
In the command window:
ca =
'row1' [ 1] [ 2] [ 3]
'row2' [14] [15] [16]
'row3' [ 7] [ 8] [ 9]
measurements =
1 2 3
14 15 16
7 8 9
sortedMeasurements =
1 2 3
7 8 9
14 15 16

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!