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

Thread Subject: functions in one file

Subject: functions in one file

From: adriano meis

Date: 22 Apr, 2008 16:56:33

Message: 1 of 3



Hi,
I know recent versions ( 6, 7) of Matlab let the programmer to put many
functions in only one file.
I have written many functions in matlab.
I put every function in a single file: "name_file.m". can it be used by
other files?
For example, in file "name1" I write the function A, which calls functions
B and C. B and C are
written in this file "name1".
In file "name2" I write the function D, which calls functions B and C.
Have I got to write functions B and C in file "name2" too?
Can file "name2" use functions B and C, also if they are written only in
file "name1"?

Thank you,
Adriano


Subject: functions in one file

From: Sven Fischer

Date: 22 Apr, 2008 17:32:01

Message: 2 of 3

Re-Hi!

"adriano meis" <doreciakgulp@tin.invalid> wrote in message
<5NoPj.294840$%k.403939@twister2.libero.it>...
> Have I got to write functions B and C in file "name2" too?
Yes, or you may put each funtion into a separate file.

> Can file "name2" use functions B and C, also if they are
> written only in file "name1"?
No.

-- copy & paste from the matlab documentation --
Primary and Subfunctions

All functions that are not anonymous must be defined within
an M-file. Each M-file has a required primary function that
appears first in the file, and any number of subfunctions
that follow the primary. Primary functions have a wider
scope than subfunctions. That is, primary functions can be
invoked from outside of their M-file (from the MATLAB
command line or from functions in other M-files) while
subfunctions cannot. Subfunctions are visible only to the
primary function and other subfunctions within their own M-
file.
--------

best wishes,
Sven

Subject: functions in one file

From: dpb

Date: 22 Apr, 2008 17:30:39

Message: 3 of 3

adriano meis wrote:
> Hi,
> I know recent versions ( 6, 7) of Matlab let the programmer to put many
> functions in only one file.
> I have written many functions in matlab.
> I put every function in a single file: "name_file.m". can it be used by
> other files?
> For example, in file "name1" I write the function A, which calls functions
> B and C. B and C are
> written in this file "name1".
> In file "name2" I write the function D, which calls functions B and C.
> Have I got to write functions B and C in file "name2" too?
> Can file "name2" use functions B and C, also if they are written only in
> file "name1"?

Read the section in the online documentation about "function name
resoluton" -- it goes through the whole process thoroughly.

--

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
primary function Sven Fischer 22 Apr, 2008 13:35:06
subfunction Sven Fischer 22 Apr, 2008 13:35:06
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