Converting a string Array into a Char array

Hello I have an array of strings called time, they all have the format: 22:39:04.502727 hours:minutes:seconds and I want to convert all of them into a number where its minutes.seconds, so how would I do that?

3 件のコメント

per isakson
per isakson 2017 年 8 月 18 日
編集済み: per isakson 2017 年 8 月 18 日
Give us an example. To what do you want to convert the string, 22:39:04.502727 ? To the number 39.04502727 ??? That's what I read.
The title, "Converting a string Array into a Char array", and the question, "to convert all of them into a number" say different things.
Leo Quintanar
Leo Quintanar 2017 年 8 月 18 日
I'm sorry my mind was moving faster than my typing, I have the a 200X1 cell called time which has the format 22:39:04.502727 and I have another 200X1 double called temp which has the format 26.404006797400000. I want to make a graph with time vs. temp. The way I wanted to do it was to convert the time array into an int array with just the minutes and seconds as a double array as in the example 39.04502727.
Walter Roberson
Walter Roberson 2017 年 8 月 18 日
If you are using somwhere around R2015b-ish or later, then you can convert your times to datetime objects and use the datetime objects as your x coordinate, without needing to convert to integers.

サインインしてコメントする。

回答 (2 件)

Sean de Wolski
Sean de Wolski 2017 年 8 月 17 日

0 投票

Use datetime and then change the Format to that which you desire.
dt = datetime('22:39:04.502727','InputFormat','HH:mm:ss.SSSSSS')
dt.Format = 'mm:ss'

1 件のコメント

Leo Quintanar
Leo Quintanar 2017 年 8 月 18 日
編集済み: Walter Roberson 2017 年 8 月 18 日
I tried that command with a little twist:
dt = datetime(time(1),'InputFormat','HH:mm:ss.SSSSS)
because I have a cell array called time that is 200X1 I can't seem to convert it into a string array maybe its the way I have the text file read into matlab. Here'es the code that reads the text file:
[date, time, temp ] = textread('C:\Users\Leo\Desktop\data.txt','%s %s %f', 200)
is there a way to still use your method on that 200X1 array. My intentions are to graph time vs. temp.

サインインしてコメントする。

per isakson
per isakson 2017 年 8 月 19 日
編集済み: per isakson 2017 年 8 月 19 日

0 投票

"My intentions are to graph time vs. temp."
[date, time, temp ] = textread('h:\m\cssm\data.txt','%s %s %f', 8 );
dt = datetime( time, 'InputFormat','HH:mm:ss.SSSSSS' );
sdn = datenum( time, 'HH:MM:SS.FFF' ); % the old way
plot( sdn, temp ), datetick
plot( dt, temp )
where data.txt contains
2017-08-19 22:39:04.502727 26.404006797400000
2017-08-19 22:40:04.502727 26.404006797400000
2017-08-19 22:41:04.502727 26.404006797400000
2017-08-19 22:42:04.502727 26.404006797400000
2017-08-19 22:43:04.502727 26.404006797400000
2017-08-19 22:44:04.502727 26.404006797400000
2017-08-19 22:45:04.502727 26.404006797400000
2017-08-19 22:56:04.502727 26.404006797400000
produces two identical(?) diagrams. I guess, there are rounding errors.
You didn't mean
plot( temp, dt )

2 件のコメント

Leo Quintanar
Leo Quintanar 2017 年 8 月 20 日
Interesting when I use:
dt = datetime( time, 'InputFormat','HH:mm:ss.SSSSSS' );
I get this error: Undefined function 'datetime' for input arguments of type 'cell'. However, this way worked:
sdn = datenum( time, 'HH:MM:SS.FFF' ); % the old way
%
plot( sdn, temp ), datetick
per isakson
per isakson 2017 年 8 月 20 日
I run this example on R2016a. Which release do you use? datetime was "Introduced in R2014b"

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2017 年 8 月 17 日

コメント済み:

2017 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by