Clear Filters
Clear Filters

Imposing variable in script

3 views (last 30 days)
V.D-C on 23 Apr 2020
Commented: Steven Lord on 23 Apr 2020
Hello everybody,
I am currently working with a script that uses many functions and sub-functions. The issue is that I have a variable defined at the beginning of the script, but it is changed at some point in one of the functions. I work with maybe 50 or more subfunctions for each of the functions and I can't narrow down where the variable is changed.
Is there a way to impose that the variable at the beginning of the script can not change later, even if it is redifined somewhere ?
I spent many hours trying to see where it changes my variables by making step-by-step and ctrl + F as much as possible, but this is taking too long to cover everything..
Thanks you in advance !
John D'Errico
John D'Errico on 23 Apr 2020
Edited: John D'Errico on 23 Apr 2020
Another level of Dante's programming hell is to inherit a lengthy code from someone, and then need to use and maintain it. Sadly, Dante the programmer has many levels of hell. What always seems to happen is the inherited code has no comments, and was written in spaghetti style, so it is impossible to understand. Or the programmer uses really meaningful variable names, like l0o1.
I once inherited a piece of code that had only one comment in the entire mess: "Create C matrix here". The author then proceeded to create a matrix called C. Gosh, thanks. I re-wrote the code from scratch.
Another time, I was given a complex piece of object oriented programming in MATLAB, but again, no documentation provided. We spent a few days trying to decypher what it did and how to use it by testing the various options, not all of which worked. Again, the answer was it took me less time to rewrite it from scratch than it would have taken to learn to use the code, all the while avoiding the various bugs.
Steven Lord
Steven Lord on 23 Apr 2020
Scripts have the benefit of accessing and/or changing any variable in the workspace from which they are called.
Scripts have the drawback of accessing and/or changing any variable in the workspace from which they are called.
As you've worked to understand the collection of scripts and how they interact, I recommend trying to convert some of them into functions that state exactly what they need to do their work (their input arguments) and exactly what they're allowed to change (their output arguments.) This isn't something that you'd need to do all at once; you could chip away the problem a script at a time.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 23 Apr 2020
Edited: John D'Errico on 23 Apr 2020
First, this sounds as if you are using globals to pass your variables around. UGH. If there is one painful thing that globals will do TO you, it is to create a hell of fiendish proportions for exactly this reason. You cannot then figure out which function has managed to screw up some variable, changing the value in some bad way. Tracking those things down is frustrating and painful. Even worse is if you are using eval in some way to create variable names on the fly - which can consign people to yet another level of Dante's programming hell.
But even if you are passing your variables around in some other, safer way, It can still be a problem to find where you have such a bug.
No. You cannot establish some variable in advance as immutable - something that can never be changed or overridden. Even if you created a function with some name, you can still then later on set a variable to have the same name, dynamically overloading your special function name. So sorry, but no, you cannot enforce a do not touch this variable rule in MATLAB.
That leaves you with debugging where it happens.
I suppose you could try to set a dbstop condition. Since you know the initial value where this variable has been set at, you could use the debugger to set a stop condition if the value of that existing variable is ever not equal to the original value. So among the dbstop options, I see this:
(10) dbstop in FILE if EXPRESSION
So I have a variable with some name: FRED. At the beginning, we have set FRED to some value.
FRED = 17;
Now, at the commandline, you will need to set a debugging stop condition.
dbstop in myfile if ~isequal(FRED,17)
You will need to supply the correct file ename, instead of the one I made up of course. If you have many independent functions in different files, then you would need to set such a stop for each file. If they are all subfunctions or nested functions in the same file, then you should need to do it only once.
Yes, it may take some effort, but it might also be a good programming lesson of how/why not to write code, in case you actually did what I suspect you may have done.
Is there anything else you could do? If the question is just one of finding every line where a certain variable name has been overwritten, then you could use a tool like mgrep. MGREP can be found on the file exchange for free download:
It can search for every line in all of your codes, where a specific string is found. MGREP just treats your m-files as if they are simple text files, then searches through them.
For example, if your variable is named FRED, then you might try something like this command in MATLAB:
mgrep 'FRED =' . casesens on
This will search every m-file in the current working directory, for the exact line where the variable FRED is assigned a new value. (The default, where the case sensitive flag is off would allow it to also find where variables named fred or Fred are changed.) As well, mgrep, by default, will search sub-directories recursively. And you can tell mgrep what top level directory to start the search from. The dot in that command tells mgrep to start the search from the current working directory.
You might want to do that call twice, the second time with no space between the name and the assignment operator.
mgrep 'FRED=' . casesens on
If you then have any line in your code where FRED is overwritten, you could then put a debugging stop on that line.
V.D-C on 23 Apr 2020
Just you know, I found the mistake with random luck, and it was tricky because the variable was overwritten but not with "variable = something". It was overwritten by reading a an excel sheet that associated another value to this variable.
But I tested your solution and it found all the strings I was searching for ! It will definitely be super useful for future works :)
Stephen23 on 23 Apr 2020
Edited: Stephen23 on 23 Apr 2020
"It was overwritten by reading a an excel sheet that associated another value to this variable"
This will not happen unless the code uses awful code practices, e.g. eval, assignin, etc.
With the exception of load, none of the standard file importing functions overwrite variables like that.

Sign in to comment.

More Answers (0)


Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!