Can the attempted addition of a new column to a timetable be intentionally prevented?
1 view (last 30 days)
Show older comments
Kevin Johnson
on 7 Jan 2023
Commented: Kevin Johnson
on 7 Jan 2023
Can the attempted addition of a new column to a timetable be intentionally prevented?
After I build a table using particular variables, my code is sufficently complex that I may inadvertantly add a new variable column to the table, for example by slightly mispelling the name of an already exiting variable there.
Can this be prevented?
Thanks
1 Comment
the cyclist
on 7 Jan 2023
The hard part will likely be the definition of what constitutes "inadvertent" vs. intentional. If you can do that, the MATLAB code is probably straightforward.
Can you upload an example table, and the rule?
Accepted Answer
Walter Roberson
on 7 Jan 2023
If you are currently using dot assignment to update columns, such as
T.prices = something
then instead write wrapper routines such as
T = new_table_var(T, 'prices', zeros(5,1));
newvalue = T.prices + 1.05;
T = change_table_var(T, 'prices', newvalue);
function T = new_table_var(T, whichvar, initialvalues)
if ismember(whichvar, T.Properties.VariableNames)
error('Attempt to re-create existing table variable "%s"', whichvar);
end
T.(whichvar) = initialvalues;
end
function T = change_table_var(T, whichvar, newvalue)
if ~ismember(whichvar, T.Properties.VariableNames)
error('Attempt to set nonexistent table variable "%s"', whichvar);
end
T.(whichvar) = newvalue;
end
I
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!