Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

2,569 views
Skip to first unread message

Henrik

unread,
Sep 13, 2007, 12:11:03 PM9/13/07
to
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

John

unread,
Sep 13, 2007, 12:20:00 PM9/13/07
to
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 " <henrik...@lanl.gov> wrote in message
<fcbnen$rdu$1...@fred.mathworks.com>...

Steven Lord

unread,
Sep 16, 2007, 12:51:42 AM9/16/07
to

"Henrik " <henrik...@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
sl...@mathworks.com


eduardo damasio da costa

unread,
Mar 13, 2009, 8:53:01 AM3/13/09
to
% main script body

[...]
%
% Here we insert the following code simulating php include behavior:
% read lines in file 'file1' and execute them as matlab commands.
% No empty lines allowed in file1.
%

fid=fopen('file1','r');
ligne=' '; % init of line
while(ischar(ligne)); % if ligne is not void do
clear ligne % clear previous value of ligne
comd=fgetl(fid); % read a line from file1
if (~ischar(comd)); % Stops if line is void
break
end
eval(comd); % execution of ligne as it was a matlab command
end
fclose(fid);

[...]
main code continues here

"Henrik " <henrik...@lanl.gov> wrote in message <fcbnen$rdu$1...@fred.mathworks.com>...

dfgdfg

unread,
Jan 11, 2010, 10:08:02 AM1/11/10
to
i have a file1.m where two functions defined.f1 and f2;
I wabt to use these functions in file file2.m.How can i do this?

Loren Shure

unread,
Jan 11, 2010, 3:28:02 PM1/11/10
to
In article <hifesi$9kq$1...@fred.mathworks.com>, A...@mathworks.com says...

> i have a file1.m where two functions defined.f1 and f2;
> I wabt to use these functions in file file2.m.How can i do this?
>

return function handles for your 2 functions when you call file1. Pass
these to file2 for use.

--
Loren
http://blogs.mathworks.com/loren

Sulaymon Eshkabilov

unread,
Feb 15, 2012, 4:32:17 AM2/15/12
to
Hello,

in such cases, i.e. to have an M-file within another M-file (e.g. File1.m) and make an inserted M-file (FileALL.m) execute without any error message is possible just entering that M-file name with .m extension. Let's say Filegiven.m can be executed within fileTEST.m having Filegiven.m within fileTEST.m. Sometimes it may result in some error messages after execution process is over.
Hence, a good way to make an inserted file (e.g: File1.m) a function file and all needed outputs from it as output arguments.
E.g:
FileALL.m contains data analysis of data obtained from File1.m.
FileALL.m contains:

% A and B are obtained from File1.m
I=fft(A); Y=fft(B);

File1.m is to be:

function [A, B]=File1(~)

% function file: File1.m reads a data from an experimental data file e.g. txt, dat, lvm, etc % containing two columns of data in floating point format.
% this is an example of data import file
FName=input('Enter a data file name incl. extension: ', 's')
Nheader=input('Enter # of header text lines');
fid1=fopen(FName);
DAT1=textscan(fid1, '%f32 %f32', 'headerlines',Nheader);
fclose(fid1);
A=DAT1{1,1}; B=DAT1{1,2};

Best luck,
SE


"Henrik" wrote in message <fcbnen$rdu$1...@fred.mathworks.com>...
0 new messages