Azzera filtri
Azzera filtri

String to raw data conversion problem (Timestamp Data)

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

Pranav Verma
Pranav Verma il 10 Nov 2020
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];
This returns a datetime array st.
To plot the data with timestamps, you can refer to the below discussion thread on the same lines:
  1 Commento
Stuart Duff
Stuart Duff il 10 Nov 2020
Hi Pranav,
Thank you very much, ill plug this in a see what I can do with the plotting afterward.

Accedi per commentare.

Più risposte (1)

Eric Sofen
Eric Sofen il 12 Nov 2020
Even 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


Scopri di più su Characters and Strings in Help Center e File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by