Convert a single column in cell array to datetime format
이전 댓글 표시
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
per isakson
2017년 12월 10일
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?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!