How to read date and time from text file and move into cell array?

1 Ansicht (letzte 30 Tage)
hello. i have a text file which contains a date and a time. I need to extract the date and time from the text file and put it in the cell array. The cell array should look like this:
Date Time 06 00 06 00
OBDMIDs ExhaustGasSensorMonitorBank1Sensor1
2016-03-18 09:21:31 OBDmonitorIDsSupported$01_$1F Supported
Please note that nothing needs to be done for rest of the columns. Its already done.
this is my code;
Cstr = textread('Test1.txt', '%s', 'delimiter', '');
cString = Cstr(29:end); %removes headers
cString = char(cString); %Transforms into a char array,...
%split the columns
pdu = cellstr(cString(:, 1));
parameter = cellstr(cString(:, 1:42));
value = cellstr(cString(:, 43:86));
%unit = cellstr(cString(:, 87:end));
for i=1:length(parameter)
temp = parameter{i};
if ismember(temp(1),num2str(0:9))
tempSplit = regexp(temp,' ', 'split');
newPDU(i) = tempSplit(1);
newParameter(i) = tempSplit(2);
else
newPDU(i) = newPDU(i-1);
newParameter(i) = parameter(i);
end
end
newc = [newPDU; newParameter; value'];
the text file is attached below. Thank you for the help in advance.

Akzeptierte Antwort

Shameer Parmar
Shameer Parmar am 20 Jun. 2016
% code for Date
Date = Cstr{3};
splitDate = strtrim(regexp(Cstr{3},' ','split'));
newDate{1} = strrep(splitDate{1},':','');
newDate{2} = '';
newDate{3} = splitDate{end};
newDate'
.
% code for Time
Time = Cstr{4};
splitTime = strtrim(regexp(Cstr{4},' ','split'));
newTime{1} = strrep(splitTime{1},':','');
newTime{2} = '';
newTime{3} = splitTime{end};
newTime'
once done, please concatenate with newc as follow..
final_newc = [newDate', newTime', newc];
  2 Kommentare
yousaf obaid
yousaf obaid am 20 Jun. 2016
Brilliant. Works again without any problem. Now just one question remaining. will post a new question. Thank you once again.
Guillaume
Guillaume am 20 Jun. 2016
I'd recommend (to both Shameer and yousaf) learning regular expressions properly. It is a complete waste of time to strtrim and strrep, the result of a regexp. regexp can do it all at once:
result = regexp(line, '(\S+)\s*:\s*(\S+)', 'tokens', 'once');
is one of the many ways of doing the same as above in just one line.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 20 Jun. 2016
Bearbeitet: Guillaume am 20 Jun. 2016
Before you remove the headers obviously:
date = regexp(Cstr{4}, '(?<=Date:\s*)\S+', 'match', 'once');
time = regexp(Cstr{5}, '(?<=Time:\s*)\S+', 'match', 'once');
At the end:
newc = [{'Date', 'Time'; '', ''; date, time}, newc];
I see you've given up on my previous solution for reading your file, which is a shame as it avoided loops entirely.
  3 Kommentare
Guillaume
Guillaume am 20 Jun. 2016
Well, you're parsing the PDU very differently and as a result requires even more parsing. You went from a clean and efficient (in my opinion) solution to one that includes an extra loop for more parsing and, if you use the overly complicated answer you've accepted here, even more parsing!
You're parsing the same data three times now. In the grand scheme of things it probably does not matter as your text file is fairly small so you won't notice the extra time spent reparsing the data.
I would recommend adding comments to questions if the solution does not work perfectly rather than asking new question. It makes it easier to follow what you're doing.
Also, I'd recommend waiting a little before accepting answers.
yousaf obaid
yousaf obaid am 20 Jun. 2016
the reason for parsing the PDU differently is because the output from your answer was like this
06 00 OBDMIDs Exhaust
OBDmonitorIDsSupported$01_$1F GasSensorMonitorBank1Sensor1 supported
While i wanted it to be like this
06 00 06 00
OBDMIDs ExhaustGasSensorMonitorBank1Sensor1
OBDmonitorIDsSupported$01_$1F supported
and so on. there were other instances of misplaced words as well for example 'TestValue' was displayed as 'testval' in one row and 'ue' in the next row. this was the only reason that i posted a new question.
and thank you for your last advice. I will definitely keep that in my mind for my further questions. Thank you.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings 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