Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: Real global variables [duplicate]

Possible Duplicate:
Declaring a global variable in MATLAB

Currently my matlab code is in one big script file with no internal functions. I would like to have functions declared within my script, to make my code more readable and reuse code etc. This in itself is not difficult in matlab, e.g.

Example 1:

function main()
  myfunc('goat');
end

function myfunc(x)
  fprintf(x);
end

My problem is that I have a biig datafile, which I want to load only once, such that I can re-run my code during development without spending time on re-loading the data. This is not a problem in my current framework with one big script with no internal functions. One solution is to have two matlab script files. One for loading data and then calling the functions in another script.

However in the above example 1 a true global variable declaration will not work, and I am forced load the biig file every time I run the script. What I would like to do can be shown in two examples:

Example 2:

% Global variable
if ~exist('data',var) 
  data = load biigdatafile.mat;  %FAILS, outside function.
end

function main()
  myfunc('goat');
end

function myfunc(x)
  fprintf(x);
end

Example 3:

function main()
  % Global variable
  if ~exist('data',var)
    global data;     % Is not really global after whole script is completed.
    data = load biigdatafile.mat;
  end
  myfunc('goat');
end

function myfunc(x)
  fprintf(x);
end

So my question is how to declare a true global variable as in example 2, where I load my data once to stay in workspace, while calling my functions inside one script?

like image 501
user1898837 Avatar asked Nov 04 '22 08:11

user1898837


1 Answers

Use input arguments, that's what they're made for

You could just use arguments to the main function, load the dataset once into the base workspace and call your function with that dataset as an argument. If any subfunctions also use the dataset, pass it along

function main(data)
    if nargin<1
        disp('hey, you forgot to supply the dataset!');
    end
    % do your stuff
    showData(data);
end

and then in the base workspace:

Data = load('biigdatafile.mat');
main(Data);

Use persistent variables

persistent X Y Z defines X, Y, and Z as variables that are local to the function in which they are declared; yet their values are retained in memory between calls to the function. Persistent variables are similar to global variables because the MATLAB software creates permanent storage for both. They differ from global variables in that persistent variables are known only to the function in which they are declared.

So you could easily use:

function main()
    persistent data;
    if isempty(data)
        disp('loading dataset');
        data=load('biigdatafile.mat');
    end
    % do your stuff
    showData(data);
end

First time you call this function on a cleared base workspace or cleared function*, the dataset will be loaded. Every next time (when the function hasn't been edited), the dataset will already/still be in memory.

I usually do this when I'm just using one dataset; it's tedious to always load the dataset and when testing a function, it's also easier to just press F5.



* when is a function cleared you might ask?

Whenever you clear or modify a function that is in memory, MATLAB also clears all persistent variables declared by that function. To keep a function in memory until MATLAB quits, use mlock.

like image 164
Gunther Struyf Avatar answered Nov 15 '22 08:11

Gunther Struyf