MATLAB Answers


Data and variable management

Asked by Tolulope on 8 May 2013
I would like to know how to pass lots of variables efficiently between functions.
At the moment, I have close to 30 m.files. All file are either functions or scripts. I have tried to organised my code as best as I can in groups of functions the perform as single task. Once a stage of analysis is completed, the code/evaluation moves to the next group of functions. For example,
1. Stage 1 group of functions, sub-functions and scripts: Loads data 2. Stage 2 group of funcs, subfuncs and scripts: Analyses loaded data 3. Stage 3 group of funcs, subfuncs and scripts: Generates some basic plots
I notice that passing so many variables between functions is getting tricky. For example, let's say there's a variable evaluated in Stage 1 that's useful in Stage 3. Now let's say it's evaluated in a third tier sub-function (a sub-function of a sub-function of a function), I would assign the variable out (often with "varargout"), all the way to top tier function, and then pass it all the way down to desired function in Stage 3.
My knowledge of MatLab is pretty basic, my knowledge of programming even less. So I have avoided using things like structures and callbacks. I only use scripts, functions and arrays.
Is there a better way of passing variables around between functions?


Sign in to comment.

3 Answers

Answer by the cyclist
on 8 May 2013
 Accepted Answer

You mention "avoiding" structures, but I would say this is definitely one solution. They are not that difficult to learn. Here is one starting point:
Another possibility, which may or may not be feasible (depending on the size of the variables), would be to write/read the variables to disk, using the save() and load() commands. Maybe not the best programming practice, but easy.
Speaking of questionable programming practices, you could also use global variables. This may be appropriate if the same variable are really shared by a lot of function.
>> doc global
for details.

  1 Comment

Yes, I too had come across warnings against using global variables and binary file systems, that was what perhaps dissuaded me using from them. But since the project does not require much user input, it may be worth looking into.

Sign in to comment.

Answer by David Sanchez
on 8 May 2013

You should start thinking on either global or struct variables....or a global struct. For example: define a global variable by typing
global my_global_var %(any name to your liking is valid)
right after the function definition.
function my_fun
global my_global_var
% function code here
Then, add my_global_var. as a prefix to all your variables: if you had a variable called xxx, rename it
Your functions/scripts can use the variables and change them. Changes will be applied globally.


Sign in to comment.

Answer by Jason Ross
on 8 May 2013

I would recommend using structures versus using globals. Global variables have the potential to cause problems which are extremely difficult to diagnose which can lead directly to bizarre behavior by your code when you have to track where something is being changed.
Don't just take my word for -- the collected wisdom that is Wikipedia gets right into why global variables are a bad idea (in any language!) in the second paragraph:


Thanks for the reply. I had a look at the Wikipedia entry.
Even if were to use a structure it'll be always be local the the function the created unless the structure is saved somehow. I could maybe create a super structure that mimics the structure of entire algorithm. That way any time I wish to save any variable from a function it'll go to specific location in the structure tree. And when I call it, I just need to remember the name of the variable and the function that created it. In fact the latter could be stored with the variable by default, so all I need to remember is the name. In this way, the structure will be a sort of ad-hoc database. But I think the super structure may take up a lot of space!?
If you are worried about space, you can check the size with the whos function.
You also don't have to do everything in one giant struct, you can pass elements in where appropriate (e.g. myFunction(mystruct.myfield)) if you don't want to ship everything in. You could also use multiple structs, too, if that might make sense.

Sign in to comment.