How to convert a string data time to a number

조회 수: 5 (최근 30일)
Dan Howard
Dan Howard 2018년 10월 31일
댓글: Peter Perkins 2018년 11월 1일
Not sure if i am going about this the right way but here goes...
Originally trying to plot data from a table until I realized the data string format is incorrect i.e '10/08/2018 6:30:32 PM' Im assuming the function wont plot the character array hence I would need to convert the string to number format.
I extracted the column from a table and converted into a cell array...
if true
% Datestring = {converttime};
end
if true
% formatIn = '';
end
if true
% datenum(DateString,formatIn);
end
Im unsure what to put in the variable formatIn? Are you telling matlab to convert using a predefined number dateformat?
Appreciate some help with this.

답변 (4개)

madhan ravi
madhan ravi 2018년 10월 31일
dt = datestr(now,'mmmm dd, yyyy HH:MM:SS.FFF AM') %an example

Star Strider
Star Strider 2018년 10월 31일
Try this:
dn = datenum('10/08/2018 6:30:32 PM', 'dd/mm/yyyy HH:MM:SS AM')
q = datestr(dn) % Check Conversion
  댓글 수: 1
Star Strider
Star Strider 2018년 10월 31일
Responding to this Answer
The documentation is absolutely opaque on using scatter with table objects. I have a timetable, table and cell arrays (that I created from the same original timetable with information from another Question), and the only way I could get it to work was to use the cell array, and then concatenate it to a numeric array.
Here, the cell array is called ‘TT_C’, the first 2 columns are datetime objects, and the rest are numeric:
figure
scatter([TT_C{:,1}], [TT_C{:,9}])
grid
Perhaps others can get it to work with table objects. It eludes me.

댓글을 달려면 로그인하십시오.


Peter Perkins
Peter Perkins 2018년 10월 31일
Don't use datenums. Convert your text to datetime:
>> datetime('10/08/2018 6:30:32 PM','InputFormat','MM/dd/yyyy hh:mm:ss a')
ans =
datetime
08-Oct-2018 18:30:32
and then just plot (at least in recent versions of MATLAB).

Dan Howard
Dan Howard 2018년 10월 31일
I think I may have asked this answer incorrectly.. I have an table array with date/time in the format as e.g '10/08/2018 6:30:32 PM'
This is saved in a variable called dateamp which contains 258268*1 values.
Im trying to plot a scatter graph by using the dateamp table variable against another table variable which holds numeric values.
Quote me if i am wrong but do I need to convert the dateamp date/time format into a cell array to be able to plot the scatter graph?
  댓글 수: 1
Peter Perkins
Peter Perkins 2018년 11월 1일
If the variable in your table is a datetime, then no, at least in recent versions. I can't recall when scatter began datetime support. In any case, the plot function has since R2014b, and you can make a scatter plot with that.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by