MATLAB: Creating a matrix with two columns of different type of data

matrixmatrix array

Hi, I have one column of data (time) as the following that their type is char: 31-Jul-2014 11:24:00 31-Jul-2014 11:19:00 31-Jul-2014 11:14:00 31-Jul-2014 11:09:00 31-Jul-2014 11:04:00 31-Jul-2014 10:59:00 and I also have a column of data (values of blood glucose concentration) for the above time as the following (their type is cell): 60 58 58 56 56 56 Please help me first how can I create a matrix with two columns of the above data and after that how can I sort them based on the time.
thank you.

Best Answer

This seems to do what you want:
DT = ['31-Jul-2014 11:24:00' % Date Time (Character)
'31-Jul-2014 11:19:00'
'31-Jul-2014 11:14:00'
'31-Jul-2014 11:09:00'
'31-Jul-2014 11:04:00'
'31-Jul-2014 10:59:00'];
Glc = {60 % Plasma Glucose (Cell)
58
58
56
56
56};
DN = datenum(DT, 'dd-mmm-yyyy HH:MM:SS'); % Convert ‘DT’ To Date Numbers
DTG = [DN [Glc{:}]']; % Concatenate
DTGs = sortrows(DTG); % Sort Rows By Date
DTGm = [datevec(DTGs(:,1)) DTGs(:,2)] % Display Result
The ‘[Glc{:}]’ creates a numeric array from the cell array (equivalent to using the ‘cell2mat’ function). It is then transposed to a column vector. The rest is straightforward.
The code produces:
DTGm =
2014 7 31 10 59 0 56
2014 7 31 11 4 0 56
2014 7 31 11 9 0 56
2014 7 31 11 14 0 58
2014 7 31 11 19 0 58
2014 7 31 11 24 0 60