How does one track a change in global variable values and error out?
조회 수: 4 (최근 30일)
이전 댓글 표시
To explain the problem:
Assume a global variable 'X' which can take only a finite/known value;
say 10,20,30 or 40;
If another value is assigned to X anywhere in the workspace, the code needs to error out. Is there a way to do this globally from a main/top file, rather than inserting checks at each assignment.
Just a thought.
Thaks
댓글 수: 0
채택된 답변
TAB
2012년 12월 10일
편집: TAB
2012년 12월 10일
% ========== You Main function===========
function YourMainFunct()
global x;
x = 10; % Just Initialization
% Your implementation
m=rand(2,2);
WriteX(m(1,1));
end
% ======= A small function to write x ========
function WriteX(x_val)
global x;
if(x_val==10 || x_val==20 || x_val==30 || x_val==0)
x = x_val;
else
error('Undefined value assigned to x');
end
end
You can use function WriteX to write the value of x from any workspace.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!