Get only selected rows by time range

조회 수: 4 (최근 30일)
Dion Theunissen
Dion Theunissen 2021년 11월 30일
답변: Hiro Yoshino 2021년 11월 30일
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?

답변 (1개)

Hiro Yoshino
Hiro Yoshino 2021년 11월 30일
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!

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by