String to raw data conversion problem (Timestamp Data)
6 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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 个)
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 Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!