Get only selected rows by time range
조회 수: 7 (최근 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?
댓글 수: 0
답변 (1개)
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!