MATLAB: How to reshape a raw data matrix

datasetrawreshapetime

Hi,
I have the following problem and I am struggling to find out a solution.
  • I have a MATLAB table that comes from a database and looks like this:
| Id | update_time | station_id | status | slots | bikes
00:00 1 25
00:00 2 15
. . .
. . .
00:00 428 19
00:01 1 27
00:01 2 18
. . .
. . .
00:01 428 14
So, the data is collected each minute for 428 stations (station_id) until 23:59 (one day of data).
I would like to reshape this matrix into one that has the Update_time in column "zero" and then it has 428 more columns being Column 1 = station_id 1, column 2 = station_id 2, …, Column 428 = station_id 428. Then, the matrix must be filled in with the "SOLTS". It should look like:
| 1 | 2 | . . . . . | 428 |
00:00 25 15 19
00:01 27 18 14
.
.
23:59 12 15 0
How should I do that?
Thanks,
Oriol

Best Answer

This sounds like a job for unstack:
newtable = unstack(t(:, {'update_time', 'station_id', 'slots'}), 'slots', 'station_id')
You may want to specify new names for the columns:
newtable = unstack(t(:, {'update_time', 'station_id', 'slots'}), 'slots', 'station_id', 'NewDataVariableNames', sprintfc('station_id%d', unique(t.station_id)))