Can the attempted addition of a new column to a timetable be intentionally prevented?
조회 수: 1 (최근 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
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
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 Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!