Convert a single column in cell array to datetime format
Show older comments
I have a cell array which I imported with textscan and the first column is a date/time stamp. I want to convert this date/time stamp to a true datetime.
The cell array (data_temp) looks like this:
"2017-01-15 13:50:46.500" "OPC.Temperature.BottomTemperature" "23"
"2017-01-15 13:50:50.203" "OPC.Temperature.BottomTemperature" "24"
"2017-01-15 13:52:06.937" "OPC.Temperature.BottomTemperature" "22"
"2017-01-15 13:52:22.578" "OPC.Temperature.BottomTemperature" "24"
I have tried:
% data_temp = datetime(data_temp(:,1),'InputFormat','yyyy-MM-dd HH:mm:ss.SSS');
This converts the whole cell array to datetime and deletes the 2nd and 3rd columns. Using
data_temp{:,1}
gives me these errors:
Error using datetime (line 510)
Invalid parameter name: 2017-01-15 13:50:50.203.
Error in parser (line 39)
data_temp = datetime(data_temp{:,1},'InputFormat','yyyy-MM-dd HH:mm:ss.SSS');
I am not sure what I am doing wrong. I am doing this so I can export the data to a sql database for archiving of a manufacturing process.
Edit:
This my full code so far
%parses .plg file from Log file
fileID = fopen('test.txt');
scan = textscan(fileID,'%s %s %s %s %s %s %s %s %s %s','collectoutput', true,'Delimiter','|');
fclose(fileID);
% combines textscan cell arrays to one cell array
scan = scan{:};
%determines number of paramater rows and counts all rows in textscan cell
%array
num_nonblank = sum(~strcmp(scan(:,10),''));
num_nonblank_plusone = num_nonblank + 1;
row_count = size(scan,1);
%makes 2 copies of the textscan cell array and convert to strings
parameters = scan;
parameters = string(parameters);
data = scan;
data = string(data);
%deletes extra columns and parameter rows from data cell array
data(:,(6:10)) = [];
data((1:num_nonblank),:) = [];
%deletes data rows from parameter cell array
parameters((num_nonblank_plusone:row_count),:) = [];
%create cell arrays for individual data types
data_temp = data;
%remove all rows not containing 'OPC.Temperature.BottomTemperature' in
%column 2
temp_string = 'OPC.Temperature.BottomTemperature';
temp_comp = data_temp(:,2) ~= temp_string;
data_temp(temp_comp,:) = [];
%remove extra information from columns 3 & 4 from data_temp
data_temp(:,(2:4)) = [];
1 Comment
per isakson
on 10 Dec 2017
Neither am I. This works here on R2016a
>> dt=datetime( '2017-01-15 13:50:50.203','InputFormat','yyyy-MM-dd HH:mm:ss.SSS')
dt =
2017-01-15 13:50:50
Comments:
- Strange that you get an error for the second row, rather than the first
- Have you looked for non-printing characters?
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!