- Strange that you get an error for the second row, rather than the first
- Have you looked for non-printing characters?
Convert a single column in cell array to datetime format
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
jcledfo2
le 10 Déc 2017
Commenté : Douglas Anderson
le 4 Avr 2022
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 commentaire
per isakson
le 10 Déc 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:
Réponse acceptée
Star Strider
le 10 Déc 2017
I would create a table from ‘data_temp’, then convert the first variable to a datetime array:
data_temp = {"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"};
data_table = cell2table(data_temp);
data_table.data_temp1 = datetime(data_table.data_temp1,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS');
10 commentaires
Star Strider
le 10 Déc 2017
Try this:
data_temp = {'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'};
temp_string = 'OPC.Temperature.BottomTemperature';
temp_comp = ~cellfun(@strcmp, data_temp(:,2), repmat({temp_string},size(data_temp,1),1));
It runs, and appears to produce the correct result. I tested it on the cell array I created from the data you posted, that I assume to be similar to the original cell array (before the string call).
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!