Thread Subject: #include in matlab?

Subject: #include in matlab?

From: Gus

Date: 13 Dec, 2007 14:15:31

Message: 1 of 9

I have an m file that's exceeding 6000 lines and I want to
split it up into many separate function files so I can more
readily browse them.

Is there any way that I can create a file and somehow
include it in the execution path so that I can still create
function handles to subfunctions in that file from another m
file? Effectively, I want something like #include that I
can use to split up a file up for a single program.

Subject: #include in matlab?

From: DanK

Date: 13 Dec, 2007 14:53:26

Message: 2 of 9

On Dec 13, 9:15 am, "Gus " <lottg.nos...@janelia.hhmi.org> wrote:
> I have an m file that's exceeding 6000 lines and I want to
> split it up into many separate function files so I can more
> readily browse them.
>
> Is there any way that I can create a file and somehow
> include it in the execution path so that I can still create
> function handles to subfunctions in that file from another m
> file? Effectively, I want something like #include that I
> can use to split up a file up for a single program.

Gus,
One way to do it is to use a switchyard function at the start of your
mfile. What I do is that I have a huge file which is called something
like: application_support.m. The application_support function at the
top of the file takes the first input to the function and uses a
switch statement to compare it against all of the subfunctions, and
then passes all the other input arguments to the subfunction. Here's
an example:

function varargout=VL200s_support(Fcn,varargin)

% VL200s_support - Support routines for VL200 & VL300 processing
% Switchyarded routine for many subroutines
% Syntax: varargout=VL200s_support(Fcn,varargin)
% Fcn - Description
% varargin - Description
% varargout - Description
% Example
% Line 1 of example
%
% Subfunctions: VL200_Gen_ProcOPTS, set_constants, filterinit,
prepareData,
% processData, calculateFirstPeak, calculateAllPeaks,
load_Cal_spectra,
% VL200_LoadFiles, applyFilter
% See also: VL200_main

%% Create a switchyard for the functions:
varargout=cell(1,nargout);
switch lower(Fcn)
case 'loadonevlfile'
[varargout{:}]=loadOneVLFile(varargin{:});
case 'gen_procopts'
[varargout{:}]=VL200_Gen_ProcOPTS(varargin);
case 'set_constants'
[varargout{:}]=set_constants(varargin);
case 'filterinit'
[varargout{:}]=filterinit(varargin);
case 'preparedata'
[varargout{:}]=prepareData(varargin{1},varargin{2},varargin{3});
case 'processdata'
[varargout{:}]=processData(varargin{1},varargin{2},varargin{3});
case 'load_cal_spectra'
[varargout{:}]=load_Cal_spectra(varargin{1},varargin{2});
case 'applyfilter'
[varargout{:}]=applyFilter(varargin{:});
case 'getrev'
[varargout{:}]=getRev(varargin{:});
end
end

then I when I want to call the applyFilter routine I use the following
syntax:

[powerSpectrum,spectrumFiltered,cleaned]=...
VL200s_support('applyFilter',spectrum,lpFilter,Const,cleaned);

where the 'applyFilter' string identifies the "real" function I'm
calling.

HTH,
Dan

Subject: #include in matlab?

From: Randy Poe

Date: 13 Dec, 2007 14:59:25

Message: 3 of 9

On Dec 13, 9:15 am, "Gus " <lottg.nos...@janelia.hhmi.org> wrote:
> I have an m file that's exceeding 6000 lines and I want to
> split it up into many separate function files so I can more
> readily browse them.
>
> Is there any way that I can create a file and somehow
> include it in the execution path so that I can still create
> function handles to subfunctions in that file from another m
> file? Effectively, I want something like #include that I
> can use to split up a file up for a single program.

One suggestion: Write your subblocks as scripts
(no FUNCTION line), so they will execute just as if they
were written inline.

function myfun(a,b,c)
block1;
block2;
block3;
end

                - Randy

Subject: #include in matlab?

From: Dan Hensley

Date: 13 Dec, 2007 18:05:09

Message: 4 of 9

Gus wrote:
> I have an m file that's exceeding 6000 lines and I want to
> split it up into many separate function files so I can more
> readily browse them.
>
> Is there any way that I can create a file and somehow
> include it in the execution path so that I can still create
> function handles to subfunctions in that file from another m
> file? Effectively, I want something like #include that I
> can use to split up a file up for a single program.

Here's another suggestion that I use a lot when I want to include a
"library" of functions included in another .m file.


Your "library":

-------------------------------------
function h = myLibrary;

h.func1 = @func1;
h.func2 = @func2;

function out = func1(in1,in2)
% your code

function varargout = func2(varargin)
% etc.
-------------------------------------


Now in your calling function:

function myFunction

% Get the handles from the library
h = myLibrary;

% use one of them

in1=1; in2='string';
out = h.func(in1,in2);




Dan

Subject: #include in matlab?

From: Gus

Date: 23 Dec, 2007 14:17:16

Message: 5 of 9

Thanks Dan,

That's an elegant implementation and pretty much what I was
looking for :)

Cheers

Subject: #include in matlab?

From: Edward

Date: 3 Dec, 2008 01:12:04

Message: 6 of 9

"Gus " <lottg.nospam@janelia.hhmi.org> wrote in message <fklqlc$8mv$1@fred.mathworks.com>...
> Thanks Dan,
>
> That's an elegant implementation and pretty much what I was
> looking for :)
>
> Cheers


I have a very similar problem (my program is only 3000 lines), but I don't understand why I can't use something more akin to #include. Is there any command that simply copies one '.m' file into another? Dividing each function into an independent file is rather annoying. I would like to something like this:

"test.m":
% Include statement for "testlib.m"
function myfunc()
add(2,3)
multiply(2,3)
end


"testlib.m"
function [out] = add(in1,in2)
out = in1+in2;
end

function [out] = multiply(in1,in2)
out = in1*in2;
end

Best,
Ed

Subject: #include in matlab?

From: Walter Roberson

Date: 3 Dec, 2008 01:32:36

Message: 7 of 9

Edward wrote:
> I don't understand why I can't use something more akin to #include. Is there any command
> that simply copies one '.m' file into another?

No, not when the .m file can contain function definitions. If it does not contain function
definitions, then you could use a "script".

> Dividing each function into an independent file is rather annoying. I would like to something
> like this:
>
> "test.m":
> % Include statement for "testlib.m"
> function myfunc()
> add(2,3)
> multiply(2,3)
> end
>
>
> "testlib.m"
> function [out] = add(in1,in2)
> out = in1+in2;
> end
>
> function [out] = multiply(in1,in2)
> out = in1*in2;
> end

Objects and methods then.


--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: #include in matlab?

From: Joaquim Luis

Date: 3 Dec, 2008 01:43:01

Message: 8 of 9

DanK <dkominsky@primephotonics.com> wrote in message <aa5dace9-d6cc-4879-a706-
> Gus,
> One way to do it is to use a switchyard function at the start of your
> mfile. What I do is that I have a huge file which is called something
> like: application_support.m. The application_support function at the
> top of the file takes the first input to the function and uses a
> switch statement to compare it against all of the subfunctions, and
> then passes all the other input arguments to the subfunction. Here's
> an example:
>
> function varargout=VL200s_support(Fcn,varargin)
>
> % VL200s_support - Support routines for VL200 & VL300 processing
> % Switchyarded routine for many subroutines
> % Syntax: varargout=VL200s_support(Fcn,varargin)
> % Fcn - Description
> % varargin - Description
> % varargout - Description
> % Example
> % Line 1 of example
> %
> % Subfunctions: VL200_Gen_ProcOPTS, set_constants, filterinit,
> prepareData,
> % processData, calculateFirstPeak, calculateAllPeaks,
> load_Cal_spectra,
> % VL200_LoadFiles, applyFilter
> % See also: VL200_main
>
> %% Create a switchyard for the functions:
> varargout=cell(1,nargout);
> switch lower(Fcn)
> case 'loadonevlfile'
> [varargout{:}]=loadOneVLFile(varargin{:});
...

> then I when I want to call the applyFilter routine I use the following
> syntax:
>
> [powerSpectrum,spectrumFiltered,cleaned]=...
> VL200s_support('applyFilter',spectrum,lpFilter,Const,cleaned);
>

Actually you can reduce the obove solution to (that's what I use)

function varargout = my_funs(fun,varargin)
% Library of some functions

if (nargout)
[varargout{1:nargout}] = feval(fun, varargin{:});
else
feval(fun, varargin{:});
end

Subject: #include in matlab?

From: Steven Lord

Date: 3 Dec, 2008 15:10:50

Message: 9 of 9


"Joaquim Luis" <jluis@--ualg--.pt> wrote in message
news:gh4o75$id1$1@fred.mathworks.com...
> DanK <dkominsky@primephotonics.com> wrote in message
> <aa5dace9-d6cc-4879-a706-
>> Gus,
>> One way to do it is to use a switchyard function at the start of your
>> mfile. What I do is that I have a huge file which is called something
>> like: application_support.m. The application_support function at the
>> top of the file takes the first input to the function and uses a
>> switch statement to compare it against all of the subfunctions, and
>> then passes all the other input arguments to the subfunction. Here's
>> an example:
>>
>> function varargout=VL200s_support(Fcn,varargin)
>>
>> % VL200s_support - Support routines for VL200 & VL300 processing
>> % Switchyarded routine for many subroutines
>> % Syntax: varargout=VL200s_support(Fcn,varargin)
>> % Fcn - Description
>> % varargin - Description
>> % varargout - Description
>> % Example
>> % Line 1 of example
>> %
>> % Subfunctions: VL200_Gen_ProcOPTS, set_constants, filterinit,
>> prepareData,
>> % processData, calculateFirstPeak, calculateAllPeaks,
>> load_Cal_spectra,
>> % VL200_LoadFiles, applyFilter
>> % See also: VL200_main
>>
>> %% Create a switchyard for the functions:
>> varargout=cell(1,nargout);
>> switch lower(Fcn)
>> case 'loadonevlfile'
>> [varargout{:}]=loadOneVLFile(varargin{:});
> ...
>
>> then I when I want to call the applyFilter routine I use the following
>> syntax:
>>
>> [powerSpectrum,spectrumFiltered,cleaned]=...
>> VL200s_support('applyFilter',spectrum,lpFilter,Const,cleaned);
>>
>
> Actually you can reduce the obove solution to (that's what I use)
>
> function varargout = my_funs(fun,varargin)
> % Library of some functions
>
> if (nargout)
> [varargout{1:nargout}] = feval(fun, varargin{:});
> else
> feval(fun, varargin{:});
> end
>

Rather than having the switchyard in the "library function", I'd build a
"function struct":


% begin my_funs.m
function S = my_funs

S.VL200s_support = @VL200s_support;
S.prepareData = @prepareData;
% etc.

function y = VL200s_support(input1, input2, etc)
...
% end my_funs.m


That way I only need to call my_funs once, to retrieve the struct; when I
want to invoke one of the functions from the "function struct", I simply
perform a struct reference and directly call the function via the function
handle.


functionTable = my_funs;
supportOutput = functionTable.VL200s_support(in1, in2, in3);


I can even use dynamic field names, if you need or want to allow the user to
select what function should be run.


z = functionTable.(selectedFunction)(...)


Another benefit of this is that if the functions whose handles you store in
the "function struct" are in separate files, things like DEPFUN will
recognize that my_funs depends on those separate files (because I'm creating
a function handle to it) whereas if you pass in the name of the function, it
may not. This is the same type of issue as the missing callback problem
described here:

http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/bq6ae_n-1.html#bq6ae_n-6

--
Steve Lord
slord@mathworks.com

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com