Avoid load filename.mat in every function-file called from a main-script-file

2 views (last 30 days)
I'm quite new to Matlab, so Im still unused to some terminology.
Now I have many functions, and every function is within their own matlab-file. The functions I call from a main script-file where i call the functions, disp their results and more.
In many of the functions I use
load datafile.mat,
loading the same datafile (that contains matrices and vectors) in many functions. Now Im looking for a way to avoid loading the datafile so many times, and just do it for all functions in just a bit of code. Maybe this is to be done from within the scripting-file? How exactly can this be done.
Also there are some lines of code I consistently use in several functions, is there a way to just have these lines of code in one place? Here is an example of the code I reuse:
[pX sX] = size(X);
Also another thing I'd like is to avoid having to call a function from within another function if that's possible? Cause some functions are "reused" several times in different functions?

Accepted Answer

Jan
Jan on 8 Mar 2015
Edited: Jan on 8 Mar 2015
You can load the data file once and provide the values by using it as input arguments of the called functions:
function YourMainFunction
Data = load('datafile.mat'); % Never LOAD without catching the output
YourFunc1(Data);
YourFunc2(Data);
...
It is the best way to create tiny values like in [pX sX] = size(X) locally. It takes just some micro-seconds, but when you debug the code or modify it, you need at least minutes to get the overview.
There is no problem with re-using functions. So please explain why you want to avoid this. Usually it is the goal of each professional programmer to re-use as many code as possible, because this reduces the time for programming and debugging.
  2 Comments
Olga Sirley
Olga Sirley on 8 Mar 2015
Edited: Olga Sirley on 9 Mar 2015
Thank you that is a relief having that clarified up a bit!
Ok so if I understand this correctly, I add that bit of code you provided into my main-"file" where I call all the functions (which in my situation are all stored in separate files in the same folder as the main-"file").
So I when call a function from within my main-"file", I've told the function from within my main-"file" that the function im calling from the external file to use data that is loaded from the datafile.mat, or in shorter words I simply use the following code in my main-"file" which tells my main-"file" to pass Data onto MyFunctionName1 which returns back externalFunction1 to my main-"file" ready for use?
Data = load('datafile.mat');
externalFunction1 = MyFunctionName1(X, Data);
disp ( externalFunction1 );
Jan
Jan on 9 Mar 2015
This looks fine. You would exactly the same when you run this:
x = 0.2;
y = sin(x)

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!