String to raw data conversion problem (Timestamp Data)

3 ビュー (過去 30 日間)
Stuart Duff
Stuart Duff 2020 年 11 月 7 日
コメント済み: Stuart Duff 2020 年 11 月 12 日
Ok so this one is bugging me a little and hopefully someone has come across this particular issue before:
I have an Arduino thats logging sensor data and i have it dumping the serial data to a log file for parsing and analysis. Part of the log data is the timestamp (as I assume many people would use.)
(Note: I am aware of the Arduino tools for matlab, I have chosen to route the data this way for specific reasons)
Now, I have the data extracted into MATLAB fine, the timestamp and associated data points drop in a table upon extraction and I am able to manipulate the raw numbers without issue. HOWEVER! The timestamp is obviously in a format that MATLAB does not immediate recognize. To compensate for this I used table2array() then extractBetween() to carve out the specific hh:mm:ss portion of the data into a new array variable.
% extract relevant time data as substring
% only hh:mm:ss required for analysis
var_timestamp_raw = data(:,1);
var_timestamp_array = table2array(var_timestamp_raw)
var_timestamp_data = extractBetween(var_timestamp_array, 13, 20)
This is where is gets problematic. The data is still sitting there like a srtring and I then removed the remaining punctuation, to get int values but there you see the problem is that the values are no longer representative of a real time stamp.
"[2020-11-07 14:35:52.417] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "
So basically, my conundrum: Take this column of data (as shown above) and be able to use it as a plotable value against the sensor data im capturing at the same time.
I have read so many variations of working with timestamps my head is honestly starting to spin as everyone has a differnet take on it.
I need as simply as is possible, the timestamp data converted into a value I can plot against... Does anyone have any ideas?

採用された回答

Pranav Verma
Pranav Verma 2020 年 11 月 10 日
Hi Stuart,
I have tried converting the string values of the timestamps into a datetime array using simple string parsing:
st = ["[2020-11-07 14:35:52.417] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "
"[2020-11-07 14:35:52.418] "];
dstmp = [];
st = erase(st,'[');
st = erase(st,']');
c = deblank(st);
for i=1:length(st)
tmp = datetime(c(i) ,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS');
dstmp = [dstmp tmp];
end
This returns a datetime array st.
To plot the data with timestamps, you can refer to the below discussion thread on the same lines:
Thanks
  1 件のコメント
Stuart Duff
Stuart Duff 2020 年 11 月 10 日
Hi Pranav,
Thank you very much, ill plug this in a see what I can do with the plotting afterward.
Cheers!

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

その他の回答 (1 件)

Eric Sofen
Eric Sofen 2020 年 11 月 12 日
Even simpler...you don't need to strip off the leading and trailing brackets. Datetime can parse formats with character literals if they're specified in the format:
>> datetime(st,"InputFormat","[uuuu-MM-dd HH:mm:ss.SSS] ")
ans =
7×1 datetime array
07-Nov-2020 14:35:52
07-Nov-2020 14:35:52
07-Nov-2020 14:35:52
07-Nov-2020 14:35:52
07-Nov-2020 14:35:52
07-Nov-2020 14:35:52
07-Nov-2020 14:35:52

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by