Looping string array into parsed timestamp

I have the following data that I would like to parse into hours:minutes:secs to plot against other data.
Here is my code that is causing issues with the indexing.
TS = ["085423","085428","085431","085502"];
TS = TS';
for i = 1:length(TS)
timestr = TS(i);
end
hours = timestr(1:2);
minutes = timestr(3:4);
secs = timestr(5:6);
formatted_time_str = [hours, ':', minutes, ':', secs];
Any insight is appreciated. Thank you.

 採用された回答

Stephen23
Stephen23 2024 年 9 月 17 日
編集済み: Stephen23 2024 年 9 月 17 日

1 投票

"... that is causing issues with the indexing."
Because you are mixing up indexing into string arrays with the characters contained within them. Not the same things at all: indexing into a string array indexes into the string array, not into the characters it contains (as you incorrectly assumed).
TS = ["085423","085428","085431","085502"]
TS = 1x4 string array
"085423" "085428" "085431" "085502"
Method one: returns a string array:
FS = extractBefore(TS(:),3)+":"+extractBetween(TS(:),3,4)+":"+extractAfter(TS(:),4)
FS = 4x1 string array
"08:54:23" "08:54:28" "08:54:31" "08:55:02"
Method two: returns a char matrix:
FS = repmat(':',numel(TS),8);
FS(:,[1:2,4:5,7:8]) = char(TS(:))
FS = 4x8 char array
'08:54:23' '08:54:28' '08:54:31' '08:55:02'

5 件のコメント

James
James 2024 年 9 月 17 日
編集済み: James 2024 年 9 月 17 日
Can this be put into the loop, as I have more than 1000 files that need to be processed?
I've only showed 4 here for simplicty.
Thank you
Stephen23
Stephen23 2024 年 9 月 17 日
編集済み: Stephen23 2024 年 9 月 17 日
"Can this be put into the loop, as I have more than 1000 files that need to be processed? I've only showed 4 here for simplicty."
Sure, put it in a loop if you so desire.
Note that that code I gave you will process any number of text elements, not just four of them.
James
James 2024 年 9 月 18 日
In trying to plot the FS data (timestamp) against numerical data I get an error message:
scatter(FS,(1:5).^2)
Error using scatter
Input arguments must be numeric, datetime, duration or categorical.
I've tried to convert the FS data to numeric using:
FS = cellstr(FS);
FS = str2num(FS);
But I get the following error:
Error using str2num
Input must be a character vector or string scalar.
Thanks
Stephen23
Stephen23 2024 年 9 月 18 日
"In trying to plot the FS data (timestamp) against numerical data I get an error message:"
If you are trying to plot with this data, then you are likely much better of using DURATION objects rather than fiddling around with text or numerics:
TS = ["085423";"085428";"085431";"085502"];
H = str2double(extractBefore(TS(:),3));
M = str2double(extractBetween(TS(:),3,4));
S = str2double(extractAfter(TS(:),4));
D = duration(H,M,S)
D = 4x1 duration array
08:54:23 08:54:28 08:54:31 08:55:02
Y = rand(size(D));
scatter(D,Y)
James
James 2024 年 9 月 18 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

リリース

R2022b

タグ

質問済み:

2024 年 9 月 17 日

コメント済み:

2024 年 9 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by