Warning while overwriting a workspace variable?

Hello all,
is there a way a notification (warning/error/log) be issue when a base workspace variable is overwritten?
Details:
I have number of constants and parameters defined in various .m scripts.
I fear there might be constants with different values in some of the files . So whenever a variable is overwritten, I want to have some alert/logging.
Is this possible?
Basically, if a variable to be added to the workspace already "exists", it (some hook function or so) should somehow notify.
Thanks

6 Comments

why not use an if condition using exist() and then assigning the variable?
Stephen23
Stephen23 on 21 Jan 2019
Edited: Stephen23 on 21 Jan 2019
"I have number of constants and parameters defined in various .m scripts."
Having "various scripts" all sharing one workspace is the start of your problems...
"I fear there might be constants with different values in some of the files ."
That is a very reasonable fear.
"So whenever a variable is overwritten, I want to have some alert/logging."
And that is entirely the wrong approach to dealing with this problem.
The actual solution is to convert your scripts into functions, and then pass any parameters as input/output arguments (possibly in a structure, if there are many). Functions are what experienced MATLAB users write to avoid exactly the problem that you described, by making use of functions' independent workspaces and encapsulated functionality.
Overwrites happen a lot with scripts and base workspace or global variables. The risk is high in any program large enough that you can't keep every line in your brain memory with your eyes closed (that is, any program too large to keep the entire program in your working memory.)
However, MATLAB has no way of detecting or preventing this as it is entirely expected when you use scripts.
Hi Walter and Stephen,
may be I should have added this information in my original question.
"I use the scripts to initialize parameters for Simulink Models." And the scripts have no other functionality other than to initialize some constants and parameters for the model.
These scripts come from different people. I am just looking for a mechanism by which I can find out whenever a workspace variable is overwritten.
S = who ; % gets all variables in cell
if any(strcmp(S,variable)
disp('variable exists')
else
disp('No variable exists')
end
But you still cannot do what you want. Wanting something is rarely sufficient for it to happen in life, even if you want that very badly. You need to do something.
So there is no warning issued when a base workspace variable is changed when you use scripts. This cannot be done in MATLAB, because running a script is something equivalent to just writing commands at the command line in the current workspace. (If you call a script while inside a function, the script operates in the function workspace.)
Have you written code (apparently a script) that the users will run and use? This is generally a really bad idea, by the way. Instead, you should learn to use functions. Or are you running code, where the users will provide input into the base workspace? The same comments apply, as to how bad of an idea that is.
In order for you to protect the base workspace, that means you need to work in a protected environment. That protected environment in MATLAB is a function. So you need to use functions in some way. I can quickly think of two that will work. Maybe a third way.
  1. Force the users to provide a function, one that returns the workspace variables necessary. You will call the function. It will be defined to return those variables of interest, and only those variables. If necessary, you can always add an appropriate function header line yourself to that file before running it.
  2. (Better) Convert YOUR operations into a function.
  3. Write a parser, wherein you programmatically verify each script before it is run. This parser would read each line of the scripts in question, checking to see if any variables of interest would have been changed. Depending on how creatively nasty are your users who supply the scripts, this parser could be very difficult to write.
The alternative is to accept the risk that the users step on your workspace variables. If you don't wish to accept that risk, you need to change your work flow in some way.

Sign in to comment.

 Accepted Answer

While you can do this for certain types of variables (create a class and have that class's delete method issue a warning when executed) I'd propose a different solution. Since you mention this is in the context of a Simulink® model, consider using one of the other data storage techniques described on this documentation page. I haven't used these techniques myself, but based on your use case it sounds like a data dictionary may be useful in sharing data between the various models the different people or groups of people have written.

More Answers (1)

A method for such warning would cause a tremendous trouble. See this script:
% Script called from base workspace:
a = 0;
for k = 1:10
a = a + 1;
end
Now each iteration would display a warning, because a is overwritten. What about the automatically created variable "ans", when an output is not caught?
Using sloppy scripts on one hand, but expecting a secure access of the calling workspace is a contradiction. If you care about reliable code and a restricted access to the base workspace, us functions. If you really need to create a set of variables in the base workspace finally (is this really required for calling Simulink?), use function to create a struct at first, and copy the fields of the struct to the workspace at the end:
function Main
S = struct([]);
S = yourFunc1(S);
S = yourFunc2(S);
PokeStructToBase(S);
end
function S = yourFunc1(S)
S.Param = 7.187;
end
function S = yourFunc2(S)
S.Value = pi + 2;
end
function PokeStructToBase(S)
% Evil! This can cause troubles if the field names are bad:
F = fieldnames(S);
for k = 1:numel(F)
assignin('Base', F{k}, S.(F{k})));
end
end

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Asked:

ES
on 21 Jan 2019

Answered:

on 21 Jan 2019

Community Treasure Hunt

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

Start Hunting!