Can the attempted addition of a new column to a timetable be intentionally prevented?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Kevin Johnson
le 7 Jan 2023
Commenté : Kevin Johnson
le 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 commentaire
the cyclist
le 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?
Réponse acceptée
Walter Roberson
le 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!