creating a matrix with two columns of different type of data
5 views (last 30 days)
Show older comments
Iman Hajizadeh
on 22 Mar 2015
Commented: Mona Al-Kharraz
on 29 Jun 2020
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.
0 Comments
Accepted Answer
Star Strider
on 22 Mar 2015
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
2 Comments
Star Strider
on 22 Mar 2015
And to convert all of it back to a cell array and display it:
DTGc = {datestr(DTGs(:,1), 'dd-mmm-yyyy HH:MM:SS') DTGs(:,2)};
celldisp(DTGc)
Mona Al-Kharraz
on 29 Jun 2020
I have two different arrays, one of them is string array and the other is double array. I want to concatenate them and sort the new matrix by the second column (double array). When I use the concept of the above solution, it consider the second column as string column and sort them as string not as double.
More Answers (0)
See Also
Categories
Find more on String Parsing 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!