Optimal Code Structuring - Global Variables

1 visualización (últimos 30 días)
sko
sko el 15 de Oct. de 2020
Respondida: Bjorn Gustavsson el 15 de Oct. de 2020
Hello to everyone,
I am an intermediate Matlab user and I dont seem to understand how to structure my code (declare or not as global variables, write a function or a script for a given task etc) in order to minimize the time for running a code.
For instance, my code works well when I define certain variables as global but I read that this is not a good idea and that I should learn to pass a variable to a function without declaring it as global.
However, I simply dont know how to do this. My code produces several important variables that I initially stored as global and therefore I have no problem in using them later in the code/ other functions etc. However, without declaring variable as a global one , I simply dont understand how to use variable obtained in one function, and manipulate with it in another function.
Can someone help me with reference on this subject?
Thank you
  2 comentarios
Stephan
Stephan el 15 de Oct. de 2020
These concepts also work for non optimization purposes
Stephen23
Stephen23 el 15 de Oct. de 2020
Editada: Stephen23 el 15 de Oct. de 2020
"...how to structure my code ... in order to minimize the time for running a code."
If you are in any way interested in efficiency (either your own time or runtime) then avoid global variables:
"Can someone help me with reference on this subject?"
By far the most efficient way to pass data is as input/output arguments. This should be your starting point:
If you need to changes values in one workspace and have those changed values available in other workspaces then nested functions can be a convenient solution.

Iniciar sesión para comentar.

Respuestas (1)

Bjorn Gustavsson
Bjorn Gustavsson el 15 de Oct. de 2020
Matalb allows you to write functions with multiple out-put variables:
function [phi_12,l_12] = a_silly_fcn(r1,r2)
phi_12 = atan2(r2(2)-r1(2),r2(1)-r1(1)); % angle
l_12 = norm(r2-r1); length
end
This you can use to return all your important results from your function. In combination with varargout and varargin you can handle calls with varying number of output and input-arguments (to handle all cases is a bit of a hassle). This is one basic gain by using functions - that you explicityl know what's returned from your functions. This silly example you can use to calculate the path-length passing through a couple of points:
r1 = randn(1,2);
r2 = randn(1,2);
r3 = randn(1,2)
[phi_12,l_12] = a_silly_fcn(r1,r2);
[phi_23,l_23] = a_silly_fcn(r2,r3);
l_tot = l_12 + l_23;
% and the bearing to go from point 1 to 2 and from 2 to 3 are:
Bearing_dirs = [phi_12,phi_23];
turning_angle = phi_23 - phi12;
When you use global variables you will run into problems where they are set or modified a long distance from where a problem or an error occurs, this makes debugging "challenging" - since the value of a global might have been modified several function-calls ago, and this happens without any trace of where. I strongly recommend that you stop using global variables right now. The long-range effects modifications of global variables has is very error-inducing and such errors are of a ghostly character...
HTH

Categorías

Más información sobre Variables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by