octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Functions in scripts


From: Rik
Subject: Re: Functions in scripts
Date: Tue, 24 Jan 2017 10:51:34 -0800

On 01/24/2017 10:25 AM, address@hidden wrote:
Subject:
Re: Functions in scripts: Matlab evolves
From:
Mike Miller <address@hidden>
Date:
01/24/2017 10:25 AM
To:
Julien Bect <address@hidden>
CC:
octave-maintainers <address@hidden>
List-Post:
<mailto:address@hidden>
Precedence:
list
MIME-Version:
1.0
References:
<address@hidden>
In-Reply-To:
<address@hidden>
Message-ID:
<address@hidden>
Content-Type:
text/plain; charset=utf-8
Message:
5

On Tue, Jan 24, 2017 at 15:56:17 +0100, Julien Bect wrote:
> Hi all,
> 
> I just learned that functions definitions are now allowed in Matlab scripts
> (>= R2016b).
> 
> But the functions definitions must be at the end of the script.
> 
> https://fr.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html
> 
> So, the following script works in Octave but fails in Matlab:
> 
> %%%%%%%%%%%%%%%%%
> 
> x = 1;
> 
> function y = f(x)
> y = 2 * x;
> end
> 
> z = f(x)
> 
> %%%%%%%%%%%%%%%%%
> 
> and this one works in Matlab (>= R016b) but fails in Octave:
> 
> %%%%%%%%%%%%%%%%%
> 
> x = 1;
> 
> z = f(x)
> 
> function y = f(x)
> y = 2 * x;
> end
> 
> %%%%%%%%%%%%%%%%%
Does the function f() exist in Matlab's workspace after the script
completes? If it is locally scoped to the execution of the script only,
that is yet another difference with Octave.

What does this return in Matlab?

%%%%%%%%%%%%
x = 1;

localfunctions ()

z = f(x)

localfunctions ()

function y = f(x)
y = 2 * x;
end
%%%%%%%%%%%%

I'm interested in when Matlab adds the function to the symbol table, and Mike's question is whether it stays in the symbol table after the script finishes.

--Rik

reply via email to

[Prev in Thread] Current Thread [Next in Thread]