formatting character arrays [duplicate]

不羁的心 提交于 2020-01-05 07:23:14

问题


Possible Duplicate:
Counting values by day/hour with timeseries in MATLAB

This is an elementary question, but I cannot find it:

I have a 3000x25 character array:

2000-01-01T00:01:01+00:00
2000-01-01T00:01:02+00:00
2000-01-01T00:01:03+00:00
2000-01-01T00:01:04+00:00

These are obviously times. I want to reformat the array to be a 3000x1 array. How can I redefine each row to be one entry in an array?

(Again, this is simple, I'm sorry)


回答1:


Other than converting to serial date numbers as other have shown, I think you simply wanted to convert to cell array of strings:

A = cellstr(c)

where c is the 3000x25 matrix of characters.




回答2:


You need to specify a format for the array and feed it to datenum, like this:

>> d = datenum(c,'YYYY-MM-DDTHH:mm:ss')
d =
  1.0e+005 *
    7.3487
    7.3487
    7.3487
    7.3487

The times are now stored as datenums, i.e. as floating point numbers representing the number of days elapsed since the start of the Matlab epoch. If you want to convert these to numbers representing the fraction of the day elapsed, you can do

>> t = d - fix(d);

and if you want the number of seconds since midnight, you can do

>> t = 86400 * (d - fix(d));
t =
   61.0000
   62.0000
   63.0000
   64.0000


来源:https://stackoverflow.com/questions/11673556/formatting-character-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!