Effacer les filtres
Effacer les filtres

Can the attempted addition of a new column to a timetable be intentionally prevented?

2 vues (au cours des 30 derniers jours)
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
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?

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
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)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by