Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: "include" a .m file into another .m file

Subject: "include" a .m file into another .m file

From: Henrik

Date: 13 Sep, 2007 16:11:03

Message: 1 of 3

Hey
I was wondering if matlab supports adding all text from a
.m-file (not a function file just code that runs buy it
self) to another .m file that I'm running.
Like php scripts that you can import anywhere in your php
file buy just typing include(FILENAME).
So I have a file test.m that is a function. I call it
test(obj) and it starts running. It comes to an "include"
statement and it just keeps running through the included
file with all it's calculations and stuff and when it comes
to the end of the file it just continues in the test file
with the line after the "include" statement.

Why I need this. I get files from another guy that is
written as just code and I like to be able to run the file
in my code without changing anything in his file since he is
updating it all the time to a CVS system...so I can't change
the file everytime he puts up a new file.

Thanks
HS

Subject: Re:

From: John

Date: 13 Sep, 2007 16:20:00

Message: 2 of 3

You mean you are running matlab scripts. All you have to do
is call the script wherever you want to 'include' it. i.e in
in test.m if you want to run the other script , say file1.m
at say line 20 just call it by typing file1 ;
Ofcourse, file1 must be in a directory thats in your path
variable, otherwise you will have to specify the full
pathname instead of just file1;

~S

"Henrik " <henrikNOSPAM@lanl.gov> wrote in message
<fcbnen$rdu$1@fred.mathworks.com>...
> Hey
> I was wondering if matlab supports adding all text from a
> .m-file (not a function file just code that runs buy it
> self) to another .m file that I'm running.
> Like php scripts that you can import anywhere in your php
> file buy just typing include(FILENAME).
> So I have a file test.m that is a function. I call it
> test(obj) and it starts running. It comes to an "include"
> statement and it just keeps running through the included
> file with all it's calculations and stuff and when it comes
> to the end of the file it just continues in the test file
> with the line after the "include" statement.
>
> Why I need this. I get files from another guy that is
> written as just code and I like to be able to run the file
> in my code without changing anything in his file since he is
> updating it all the time to a CVS system...so I can't change
> the file everytime he puts up a new file.
>
> Thanks
> HS
>

Subject: "include" a .m file into another .m file

From: Steven Lord

Date: 16 Sep, 2007 04:51:42

Message: 3 of 3


"Henrik " <henrikNOSPAM@lanl.gov> wrote in message
news:fcbnen$rdu$1@fred.mathworks.com...
> Hey
> I was wondering if matlab supports adding all text from a
> .m-file (not a function file just code that runs buy it
> self) to another .m file that I'm running.
> Like php scripts that you can import anywhere in your php
> file buy just typing include(FILENAME).

No, not directly.

If you're trying to call the main function of another file inside your first
file, just call it like any other MATLAB function.
If you're trying to call a subfunction in the second file from within your
first file, you can have the main function in the second file be a
"switchyard" that accepts the name of the subfunction to call:

% begin switchyard.m
function y = switchyard(fcn, x)
% Call as:
% y = switchyard('mycos', 1:10);
% or
% y = switchyard('mysin', pi);

y = feval(fcn, x);

function y = mycos(x)
y = cos(x);
function y= mysin(x)
y = sin(x);
% end switchyard.m

or you can have the main function return a handle to the subfunction and
call them that way:

% begin createhandles.m
function s = createhandles
% Call as:
% s = createhandles
% y1 = s.mycos(1:10);
% y2 = s.mysin(pi);

s.mycos = @mycos;
s.mysin = @mysin;

function y = mycos(x)
y = cos(x);
function y= mysin(x)
y = sin(x);
% end createhandles.m

If the other file is a script, just run it by typing the name of the script.

--
Steve Lord
slord@mathworks.com


Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
include Henrik 13 Sep, 2007 12:15:08
m files Henrik 13 Sep, 2007 12:15:08
text Henrik 13 Sep, 2007 12:15:08
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
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 Disclaimer prior to use.
Related Topics