How to access specific integer values from a String and Convert into Integers?

2 ビュー (過去 30 日間)
Joyjit Chatterjee
Joyjit Chatterjee 2018 年 11 月 16 日
編集済み: Stephan 2018 年 11 月 16 日
I have a MATLAB String "2018-04-24 04:30:00". I would like to access two specific values and save them as integers, i.e.
year = 2018
month = 04
I tried using str2num but it truncates the entire string because I can't apply it directly to the original string in that sense. My ultimate aim is to access the integer values of 2018 and 04 from this very string and save these into 2 seperate integer type variables - year and month. Any help in this regard from the MATLAB Community would be highly appreciated.
Thanks!

採用された回答

Stephan
Stephan 2018 年 11 月 16 日
編集済み: Stephan 2018 年 11 月 16 日
To get numerical results (integer values) use ymd function:
A = "2018-04-24 04:30:00"
[Year, Month, Day] = ymd(datetime(A))
gives:
A =
"2018-04-24 04:30:00"
Year =
2018
Month =
4
Day =
24
For only month and day use:
[~, Month, Day] = ymd(datetime(A))
The answer above returns the result as datatype datetime.
Best regards
Stephan

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 11 月 16 日
編集済み: madhan ravi 2018 年 11 月 16 日
str="2018-04-24 04:30:00"
month=datetime(str,'Format','MM')
year=datetime(str,'Format','yyyy')

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by