How can I declare lots of global variables in one m-file and make them visible for all my functions in all other m-files?
조회 수: 48 (최근 30일)
이전 댓글 표시
I was thinking about putting all my global variables in one m-file in order for me to have an overview on my global variables.
I know that you should declare global variables in each and every function, because each function has its own workspace. I know that declaring a variable with the keyword global in each function makes a copy of the same variable and changes made on the copies are like made on the original...
This is fine as long as I have few global variables and few functions, but I have many variables, which I would like to introduce them to many functions... Later I will adopt an object oriented style, but first I need to analyze the program.
If I would like to declare all my global variables in a separate script or m-file, how should this be done? And how do I make these variables visible in the workspace of many other functions in many other m-files?
Thank you for your help and support in advance!
댓글 수: 2
John D'Errico
2017년 4월 3일
편집: John D'Errico
2017년 4월 3일
Zillions of global variables are just bad news. Bad, lazy programming style. They will give you buggy code, because you will have one heck of a bad time figuring out where some variable got stepped on. Sorry, but true. Pure hell to debug when you have problems.
Stephen23
2017년 4월 4일
편집: Stephen23
2017년 4월 4일
Lets have a look at what the people who write MATLAB say about globals:
Or what other people say about using globals:
The last page has this very nice quote: "I disagree that it's fine for beginners to use eval and global. These are of course the easy way out, but you never get better at anything by taking the easy way out and you don't learn how to do things in the best matter."
"Thus I would say it's especially important that beginners specifically don't use eval, globals etc. since they are not seasoned enough to know the dangers of these tools and the fact that there are better things they could learn."
And a comment on the question: "And how do I make these variables visible in the workspace of many other functions in many other m-files?"
You don't. That is a terrible way to write code. A function is a black box: it has clearly defined input and output arguments (which can be checked and debugged easily). What happens inside the black box (i.e. what variables) is irrelevant to the outside world.
Global variable is the opposite of good programming: it makes your variables impossible to keep track of when they change, and very hard to debug. Beginners sometimes want to make all workspaces access the same variables and try to use globals to achieve this: this is a guaranteed way to write buggy spaghetti code.
Learn to pass data in a more efficient and robust way:
채택된 답변
Jan
2017년 4월 3일
Putting a bunch of variables in one M-file and sharing them as globals is the best way to loose the overview. As soon as they are changed in any subfunction, even if it happens by a typo, the debugging gets horrible.
How do you keep the overview at your home? Do you store each item at a location, where it belongs (forks in the cupboard in the kitchen, screw-driver on the work bench?) or do you collect all item in front of your house on the street, one beside the other?
Start an object oriented programming style now, not later. Store the details locally only and hide it from the outside. Hide the details, such that only the well defined interface of code blocks is visible from the outside. Then you can be sure, that a working part of your code cannot be disturbed from anywhere. And vice versa you can modify the internal details of a function however you want, as long as the interface to the outside remains as defined.
Keep the code simple and clean. Avoid globals and even more a bunch of globals.
댓글 수: 6
Steven Lord
2017년 4월 4일
Instead of putting your forks and screwdrivers either on the street or on the table, put them in a drawer. That way they're organized in one place and you can easily extract what you need.
In MATLAB, two possible types of drawers that you can use are cell arrays and struct arrays. For this I'd probably use a struct array, since it gives you the ability to (or rather requires you to) name the tools it contains. That also gets you part of the way to object-oriented code (in fact in the old object-oriented programming system, objects were structs that had had the class function called on them using a certain syntax inside a constructor.) When or if you decide to go full OO, the fields in your structs can become properties or methods of your objects.
추가 답변 (1개)
Walter Roberson
2017년 4월 3일
"I know that declaring a variable with the keyword global in each function makes a copy of the same variable and changes made on the copies are like made on the original..."
Not actually a copy: all of the local versions use the same data and metadata pointers.
"If I would like to declare all my global variables in a separate script or m-file, how should this be done?"
Put them in a script and invoke the script from each routine.
Note: this will not work for any function that has an "end" that matches its "function" statement.
This approach is not recommended !!
댓글 수: 10
Steven Lord
2017년 4월 5일
"Poofing" variables into a workspace via eval or running a script file is rarely a good idea, as has been discussed repeatedly and at great length in Answers.
In some circumstances, poofing is allowed even if the function into which the variables are being poofed end with an end. As written, Walter's examples fall into these circumstances.
In some cases, including where the function being poofed into is nested in another function or has another function nested in it, it is not allowed. The third timesn example I posted is an example of such cases.
Walter Roberson
2017년 4월 5일
My understanding had been that poofing was not allowed in any function that was closed with an "end"; I had thought the matching "end" was enough to signal the workspace was to be a static workspace.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!