Including external file is making workspace messy
3 views (last 30 days)
Show older comments
I have created a file named PLOT_STANDARDS.m put it in a folder and added that to path to include it in other files.
But when I include that file at the start of another script in the following manner:
%=================script begins here
clear; clc; close all;
% at the very beginning itself I include my file
PLOT_STANDARDS % this has lots of variables defined for easy access in any other script
%=============
% rest of the script goes here
My file PLOT_STANDARDS has a lot of variables which I have created to use as my defaults.
But including it in the above manner brings all those to the current workspace. This is very problematic as now I cannot find the variables of the current script easily. They get lost in the huge pile of variables that are imported from PLOT_STANDARDS.
I do not want to import specific variables from PLOT_STANDARDS instead I want to use any of the variable defined in there at will.
How to include PLOT_STANDARDS or access the variables in it in such a way that does not fill my workspace.
3 Comments
Rik
on 8 Oct 2021
You don't need to save your variables in a mat file, just store them in a struct. By having it be the output of your function you can even store it to a variable with a very short name. Typing s.var is not much more of a problem than typing var. Added bonus is that it is clear whether you are working with the loaded default or with a local copy.
Accepted Answer
Rik
on 8 Oct 2021
- Use functions, not scripts. Each function should have documentation explaining the purpose, inputs, and outputs. That way you don't have to worry about implementation details, which will also allow you to update code without worrying about whether the variables you're using might affect other scripts.
- If you have a function that sets a lot of default values, let it return a struct. That way it is also clear where values come from.
- You might want to consider changing your PLOT_STANDARDS to a class. That way you can import all default values the same way as a struct, but if you want to change settings you can have methods that check whether the new combination of settings is possible. In a way you can think of a class as a smart struct.
- Don't use clear,clc,close all. It is an example of cargo cult programming and a code smell.
3 Comments
Stephen23
on 8 Oct 2021
Edited: Stephen23
on 8 Oct 2021
"In general why is it bad practice?"
Slow and uneccesary.
In almost all situations that beginners clear variables it is not required, but just slows down the code for no benefit. Using function workspaces is robust, repeatable, and ultimately a more efficient way to control the scope of variables.
Rik
on 8 Oct 2021
You can keep the workspace clean by using functions, and you can make sure you have a clean figure by creating one and using the handle in the rest of your function.
I also use these functions sometimes, but only during debugging will they ever appear at the top (which is also the only circumstance under which I will use a script instead of a function).
More Answers (0)
See Also
Categories
Find more on Variables 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!