Take string input and reformat as cell array

2 Ansichten (letzte 30 Tage)
Connor Brackley
Connor Brackley am 10 Jun. 2020
Kommentiert: Connor Brackley am 10 Jun. 2020
I would like to convert a string into a cell array. Here is a small example of string:
[{"ts": "2018-07-03T06:30:07", "v": 1.0}, {"ts": "2018-07-03T06:30:12", "v": 0.0}, {"ts": "2018-07-03T08:41:06", "v": 1.0}]
I would like to convert this into the following format:
[ datetime ] [number]
The ts and v refer to timestamp and value, and are not important to keep.
I believe using the textscan is an efficient way to do it, however I cannot figure out how to use it properly, any help is appreciated!
  2 Kommentare
Stephen23
Stephen23 am 10 Jun. 2020
@Connor Brackley: please upload a sample of your data by clicking the paperclip button.
Connor Brackley
Connor Brackley am 10 Jun. 2020
Here is a sample, thank you

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 10 Jun. 2020
Bearbeitet: Stephen23 am 10 Jun. 2020
>> str = fileread('SampleData.txt');
>> str = regexprep(str,{'",','[":{}\[\]]+'},{' ',''});
>> fmt = '%*s%{yyyy-MM-dd''T''HHmmss}D%*s%f';
>> out = textscan(str,fmt,'EndOfLine',',');
Checking the output:
>> out{1}
ans =
2018-07-01T101102
2018-07-01T101107
2018-07-01T101109
2018-07-01T101114
2018-07-01T101120
2018-07-01T101125
etc.
  2 Kommentare
Stephen23
Stephen23 am 10 Jun. 2020
Do you have any control over the file format?
If it was possible to change the character between each set of {...} (e.g. to ';' or similar) then it would be possible to use textscan directly on the file itself, which would likely be much faster.
Connor Brackley
Connor Brackley am 10 Jun. 2020
No, this format is direclty from an API of a service being used to store the data. For my purposes this is fast enough, but thank you for trying to improve it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ameer Hamza
Ameer Hamza am 10 Jun. 2020
Bearbeitet: Ameer Hamza am 10 Jun. 2020
Try this
str = fileread('test.txt');
dts = regexp(str, '"ts": "([0-9:T-]*)"', 'tokens');
vs = regexp(str, '"v": ([0-9.]*)', 'tokens');
dts = cellfun(@(x) datetime(x, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss'), [dts{:}]);
vs = cellfun(@str2double, [vs{:}]);
T = table(dts.', vs.', 'VariableNames', {'DateTime', 'V'});
disp(T)
Result
DateTime V
____________________ _
03-Jul-2018 06:30:07 1
03-Jul-2018 06:30:12 0
03-Jul-2018 08:41:06 1
  1 Kommentar
Connor Brackley
Connor Brackley am 10 Jun. 2020
Thank you! This worked, however @Stephen Cobeldick's answer performed the task much faster. I had 400,000+ datapoints to process so speed was important :)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by