Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
functions and scripts

Subject: functions and scripts

From: Emma Robertson

Date: 10 Nov, 2011 00:58:13

Message: 1 of 5

Hi all,

I have a problem calling a "function" file within a "script" file and would appreciate any advice.

My main running code is a script which consists of several other scripts and functions:
*****
script1
script2
script3 contains two function files which make use of parameters from script 3 and before.
*****
The problem is: the variables/parameters within the 'function' file are not recognised by Matlab, even though they have been declared in previous scripts (it is because, as I understand, variables within a function are local). Any ideas how I can get round this?

Many thanks,
Emma

Subject: functions and scripts

From: Nasser M. Abbasi

Date: 10 Nov, 2011 01:33:14

Message: 2 of 5

On 11/9/2011 6:58 PM, Emma Robertson wrote:
> Hi all,
>
> I have a problem calling a "function" file within a "script" file and would appreciate any advice.
>
> My main running code is a script which consists of several other scripts and functions:
> *****
> script1
> script2
> script3 contains two function files which make use of parameters from script 3 and before.
> *****
> The problem is: the variables/parameters within the 'function' file are not
>recognised by Matlab, even though they have been declared in previous scripts
>(it is because, as I understand, variables within a function are local).
> Any ideas how I can get round this?
>
> Many thanks,
> Emma


what do you mean by

   "script3 contains two function files"

How can a script file contains 2 other files?

Or you mean a script has 2 functions defined in it? as in

----------------

% i am a script

x=2

function r=foo(i)
   r=i^2
end

-------------------

The above is not even legal matlab. You should have got an error
if you tried to save the file?

A file in matlab can either be a 'function file' or a 'script file', but
not both. You can't define functions inside a script file.

Or may be I misunderstood your problem.

--Nasser

Subject: functions and scripts

From: Emma Robertson

Date: 10 Nov, 2011 08:36:11

Message: 3 of 5

> Or you mean a script has 2 functions defined in it?

I mean the script calls two functions that are saved in the same directory and uses them to calculate "something" using values already defined/constructed at the beginning of the script file.

> The above is not even legal matlab. You should have got an error
> if you tried to save the file?

I'm not writing the function file within the script file - I'm simply calling it from there.
The only error I got is, matrix xyz undefined, parameter xyz undefined and so on.

> A file in matlab can either be a 'function file' or a 'script file', but
> not both. You can't define functions inside a script file.

Is it allowed to call a function file from a script?

How can I get round the parameters being undefined? Would I be right in doing "global all parameters"?

Thanks
PS: if the problem is still unclear I'm willing to post my code here

Subject: functions and scripts

From: Nasser M. Abbasi

Date: 10 Nov, 2011 08:49:35

Message: 4 of 5

On 11/10/2011 2:36 AM, Emma Robertson wrote:

>
> Is it allowed to call a function file from a script?
>

yes ofcourse it is allowed to call a function from a script.

> How can I get round the parameters being undefined? Would I be right in doing "global all parameters"?
>
> Thanks
> PS: if the problem is still unclear I'm willing to post my code here

Yes, it would help to post the code (if not too large).

--Nasser

Subject: functions and scripts

From: Sebastian Hölz

Date: 10 Nov, 2011 08:55:25

Message: 5 of 5

Hi Emma,

I would really recommend to take a look at the Matlab help to get a basic understanding on functions / scripts / scope of variables etc. A good point to start is the section "Getting Started" in the Matlab help. One of the subsections deals with scripts and functions:

Matlab -> Getting Started -> Programming -> Scripts and Functions

Take your time to read this and try out the examples. This time is well invested, because it will really help you understand.

Cheers

Sebastian

Tags for this Thread

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.

Contact us