"relative global" variables in Matlab or other languages

I am engaged in scientific image processing in MATLAB, which requires solving strange optimization problems with many parameters that are used by the "experiment" function, which has many different "auxiliary functions" that use various subsets of parameters. Passing these parameters around is a pain, and I would like to have an elegant extensible solution.

My preferred solution would be a "relative global" variable - common to the "main" workspace of functions, as well as any sub-functions that the main function calls, and which indicate that they want to use this variable. But a relative global variable will not exist outside the function declaring it, and will disappear after the return of the main function.

The code would look like this if there weren’t many more experiments, each of which used the parameters differently:

function y = experiment(p1,p2,p3,...) % declare relative global variables. % these will not change value within this experiment function, % but this experiment will be reused several times in the calling function, % each time with different parameter values. relative global p1, p2, p3 ... % load and process some data based on parameters ... % call the helper + return y = experiment_helper(y,z); end function y = experiment_helper(y,z) relative global p1, p3 %% do some stuff with y, z, possibly using p1 and p3, but not changing either of them. ... end 

I understand that you can get the desired behavior in several other ways - you can pass parameters to subfunctions caused by the experiment, you can put parameters in the parameter structure and pass them to subfunctions, and so on. The first is terrible because I have to change all the arguments of the subfunctions every time I want to change the use of parameters. The second one is all right, but I have to include the structure prefix every time I want to use variables.

I believe that the “right” solution to my problem is to use parameter structures similar to what Matlab does in its own optimization code. I just wonder if there is a smoothing method that does not suggest that I type "paramStruct.value" every time I want to use the value parameter.

I understand that relative global variables can cause all kinds of nightmares for debugging, but I do not think that they would be necessarily worse than those that were caused by existing global variables. And they, at least, would have more localization than unskilled global ones.

So, how does a real programmer deal with this problem? Is there an elegant, easy to create and maintain design for this kind of problem? Can this be done in Matlab? If not, how would you do it in another language? (I always feel guilty for using Matlab, because it does not contribute to good design. I always want to switch to python, but I can not justify rethinking things and migrating the code base - unless it makes me much faster and better than a programmer for several weeks, and Matlab-specific tools, such as wavelet and optimization toolbars, can be easily or easily found or duplicated in python.)

+4
source share
2 answers

No, I don’t think Matlab has exactly what you are looking for.

The best answer depends on whether your main problem depends on the amount of input needed to pass parameters around, or if you are worried about memory usage when passing through large data sets. There are several approaches that I have taken, and they all have pros and cons.

  • Parameters in the structure. You have already mentioned this in your question, but I would like to strengthen it as an excellent answer in most cases. Instead of calling my paramStruct parameter paramStruct I usually call it p . It is always locally bound to the function I use, and then I need to enter p.value instead of value . I believe that the extra two characters are well worth the opportunity to easily go through the full set of options.

  • InputParser I often use the InputParser class when developing functions that require a large number of inputs. This really agrees well with (1) that you can pass in a parameter structure or you can use param / value pairs, and yuo can let your function define default values. Very useful.

  • Nested functions can help in limited cases, but only when all your functions can be defined hierarchically. This limits code reuse if one of your internal functions can be common enough to support multiple parent functions.

  • Passing by reference using descriptor classes. If your concern is memory. The new form classes actually allow you to define the revalbert structure that follows the link. The code looks something like this:

     classdef paramStructure < handle properties (SetAccess = public, GetAccess = public) param1 = []; param2 = []; % ... param100 = []; end end 

    Then you can create a new cross-reference structure like this

     p = paramStructure; 

    And set the values

     p.param1 = randn(100,1); 

    As soon as this is transmitted, the entire transfer is carried out by reference, and the pros (less memory usage, allows you to use some coding styles) and cons (usually, it’s easier to make some kinds of mistakes.

  • global I try very hard to avoid them, but this is an option.

+2
source

What you are describing is exactly available in MATLAB using nested functions. They have their own workspace, but they also have access to the workspace of the parent function in which they are nested. Thus, the parent function can determine your parameters, and nested functions can see them, because they have a common area.

This is a pretty elegant way of programming and causes far fewer debugging problems than global ones. Recent versions of MATLAB even highlight common variables in the editor light blue to help with this.

+1
source

Source: https://habr.com/ru/post/1381906/


All Articles