Get only selected rows by time range
5 views (last 30 days)
Show older comments
I have the folowing script:
clear all; close all; clc;
% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 40);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ";";
% Specify column names and types
opts.VariableNames = ["ID", "DrivingStyleCoastingDifficultyLevel", "DrivingStyleCruiseControl", "DrivingStyleCruiseControlDifficultyLevel", "DrivingStyleIdling", "DrivingStyleIdlingDifficultyLevel", "DrivingStyleTotalDifficultyLevel", "EndDate", "DrivingStyleCoasting", "Safety", "TotalTimeOverZero", "DrivingStyleTotal", "DrivingStyleAccelerationWeightingFactor", "DrivingStyleBreakingWeightingFactor", "DrivingStyleCoastingWeightingFactor", "DrivingStyleCruiseControlWeightingFactor", "DrivingStyleIdlingWeightingFactor", "StartDate", "DrivingStyleBreakingDifficultyLevel", "DrivingStyleBreaking", "DrivingStyleAccelerationDifficultyLevel", "VinNumber", "DriverIdentification", "FuelConsumptionLiter", "FuelConsumptionExpectedLiter", "FuelConsumptionAverage", "FuelSavedLiter", "FuelSavedCo2", "FuelSavedCurrency", "DistanceKm", "AverageSpeedKmHour", "EstimatedWeight", "ApiWebserviceId", "TrafficType", "WeatherCelsius", "WeatherType", "DateProcessed", "DrivingStyleAcceleration", "DrivingStyleTotalWeightingFactor", "BulkInsertSessionId"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "string", "double", "double", "double", "double", "double", "double", "double", "double", "double", "categorical", "double", "double", "double", "categorical", "double", "double", "string", "double", "double", "double", "double", "double", "double", "double", "double", "string", "double", "categorical", "datetime", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["EndDate", "FuelConsumptionExpectedLiter", "TrafficType"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["EndDate", "StartDate", "VinNumber", "FuelConsumptionExpectedLiter", "TrafficType", "WeatherType"], "EmptyFieldRule", "auto");
opts = setvaropts(opts, "DateProcessed", "InputFormat", "yy-MM-dd");
opts = setvaropts(opts, "DriverIdentification", "TrimNonNumeric", true);
opts = setvaropts(opts, ["ID", "DrivingStyleCoastingDifficultyLevel", "DrivingStyleCruiseControl", "DrivingStyleCruiseControlDifficultyLevel", "DrivingStyleIdling", "DrivingStyleIdlingDifficultyLevel", "DrivingStyleTotalDifficultyLevel", "DrivingStyleCoasting", "Safety", "TotalTimeOverZero", "DrivingStyleTotal", "DrivingStyleAccelerationWeightingFactor", "DrivingStyleBreakingWeightingFactor", "DrivingStyleCoastingWeightingFactor", "DrivingStyleCruiseControlWeightingFactor", "DrivingStyleIdlingWeightingFactor", "DrivingStyleBreakingDifficultyLevel", "DrivingStyleBreaking", "DrivingStyleAccelerationDifficultyLevel", "DriverIdentification", "FuelConsumptionLiter", "FuelConsumptionAverage", "FuelSavedLiter", "FuelSavedCo2", "FuelSavedCurrency", "DistanceKm", "AverageSpeedKmHour", "EstimatedWeight", "ApiWebserviceId", "WeatherCelsius", "DrivingStyleAcceleration", "DrivingStyleTotalWeightingFactor", "BulkInsertSessionId"], "DecimalSeparator", ",");
opts = setvaropts(opts, "DriverIdentification", "ThousandsSeparator", ".");
% Import the data
scoretabel = readtable("/Users/diontheunissen/Downloads/score-tabel-20211130-1050.csv", opts);
t1 = datetime('09/11/2021 00:00');
t2 = datetime('29/11/2021 00:00');
S = timerange(t1,t2);
scoretabel2 = scoretabel(S,:);
Now i get the following error:
Error using ScoreTabelGBT (line 32)
Subscripting using TIMERANGE is only supported for selecting rows of a timetable.
How can I solve this error?
0 Comments
Answers (1)
Hiro Yoshino
on 30 Nov 2021
This is because the table you're applying the timerange object is "table". It should be "timetable" format.
To convert it to timetable, you can use table2timetable.
You may need to add a few lines of code to make this change. Good luck!
0 Comments
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!