Can't assign VariableUnits to TimeTable

3 次查看(过去 30 天)
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