How does one track a change in global variable values and error out?
12 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
TAB
on 10 Dec 2012
Edited: TAB
on 10 Dec 2012
% ========== 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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!