Can't assign VariableUnits to TimeTable

조회 수: 5 (최근 30일)
Captain Karnage
Captain Karnage 2022년 8월 25일
댓글: Star Strider 2022년 8월 25일
I'm trying to create a timetable for a motor simulation. I would like to keep track of the units of each variable.
Here is my code, a script in a file called CreateTT.m:
time.start = duration(0,0,0,0); %Start: 0m, 0s, 0ms
time.end = duration(0,1,0,0); %end: 1m
time.step = duration(0,0,0,1); %step: 1 ms
time.numsteps = (time.end - time.start) / time.step;
varNames = {'Angle','Angular Speed','Voltage','Current','Magnetic Field'};
varTypes = {'double','double','double','double','double'};
varUnits = {'degrees','deg/s','Volts','Amps','Tesla'};
data = timetable('Size', [time.numsteps, 5], 'VariableTypes', varTypes, 'TimeStep', time.step, 'VariableNames', varNames, 'VariableUnits', varUnits);
Error using timetable
Invalid parameter name: VariableUnits.
When I run it, I get the following error:
Error using timetable (line 335)
Invalid parameter name: VariableUnits.
Error in CreateTT (line 11)
data = timetable('Size', [time.numsteps, 5], 'VariableTypes', varTypes, 'TimeStep', time.step, 'VariableNames', varNames, 'VariableUnits', varUnits);
I am using R2021a, Update 7. As you can see above, I ran the code using the "Run" button here in the forum and got the same error.
I checked the online documentation for making a timetable and 'VariableUnits' is definitely a valid parameter to use. When I omit that part, my code runs fine, and if I check data.Properties, it shows 'VariableUnits' as one of the properties (albeit blank, of course).
What am I doing wrong?

채택된 답변

Star Strider
Star Strider 2022년 8월 25일
For some reason, it doesn’t like 'VariableUnits' as part of the initial timetable declaration.
This alternative approach works —
time.start = duration(0,0,0,0); %Start: 0m, 0s, 0ms
time.end = duration(0,1,0,0); %end: 1m
time.step = duration(0,0,0,1); %step: 1 ms
time.numsteps = (time.end - time.start) / time.step;
varNames = {'Angle','Angular Speed','Voltage','Current','Magnetic Field'};
varTypes = {'double','double','double','double','double'};
varUnits = {'degrees','deg/s','Volts','Amps','Tesla'};
data = timetable('Size', [time.numsteps, 5], 'VariableTypes', varTypes, 'TimeStep', time.step, 'VariableNames', varNames);
data.Properties.VariableUnits = varUnits;
Check = data.Properties.VariableUnits
Check = 1×5 cell array
{'degrees'} {'deg/s'} {'Volts'} {'Amps'} {'Tesla'}
.
  댓글 수: 2
Captain Karnage
Captain Karnage 2022년 8월 25일
Star - thank you for the workaround. After posting, I looked at timetable.m, and it appears that 'VariableUnits' (and a few others) is missing from the cell array used to check the name / value pairs, despite being mentioned as valid in the comments in multiple places. I think this is a bug.
Star Strider
Star Strider 2022년 8월 25일
It very well might be a bug. I had similar problems creating a table recently, although with a different name-value pair. I used the same approach as with your problem.
That aside, the workaround seems to produce the desired result.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Timetables에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by