Can't assign VariableUnits to TimeTable

7 views (last 30 days)
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?

Accepted Answer

Star Strider
Star Strider on 25 Aug 2022
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 Comments
Captain Karnage
Captain Karnage on 25 Aug 2022
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 on 25 Aug 2022
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.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!