フィルターのクリア

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

2 ビュー (過去 30 日間)
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 件のコメント
the cyclist
the cyclist 2023 年 1 月 7 日
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?

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2023 年 1 月 7 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by