Placing Functions Inline to Script

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Placing Functions Inline to Script

Fritz Sonnichsen
I am a new octave user (matlab refugee) and trying to get through a few
very basic problems to get going. I am trying to define functions within
a script as shown in the code below. The script fails claiming it cannot
find the function. The only workaround is to make the main script a
function as well but this has some repercussions regarding preserving
variables. Short of having separate files running around with my
functions is there a way to help the script "see" the function definition?

Thanks
Fritz

%%%%%%%%%% script %%%%%%%%
maxpoint(3);

%%%%%%%% functions %%%%%%%
function maxpoint (p)
printf("point= %d",p);
endfunction

error: 'maxpoint' undefined near line 2 column 1


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Placing Functions Inline to Script

jbect
Le 08/06/2017 à 16:03, Fritz Sonnichsen a écrit :
> %%%%%%%%%% script %%%%%%%%
> maxpoint(3);
>
> %%%%%%%% functions %%%%%%%
> function maxpoint (p)
> printf("point= %d",p);
> endfunction

Hi Fritz,

I think that the function definition must appear before you use it. Like
that:

1;  % this is a script ;-)

function maxpoint (p)
printf("point= %d",p);
endfunction

maxpoint(3);

HTH,
Julien

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Placing Functions Inline to Script

Doug Stewart-4
In reply to this post by Fritz Sonnichsen


On Thu, Jun 8, 2017 at 10:03 AM, Fritz Sonnichsen <[hidden email]> wrote:
I am a new octave user (matlab refugee) and trying to get through a few very basic problems to get going. I am trying to define functions within a script as shown in the code below. The script fails claiming it cannot find the function. The only workaround is to make the main script a function as well but this has some repercussions regarding preserving variables. Short of having separate files running around with my functions is there a way to help the script "see" the function definition?

Thanks
Fritz

%%%%%%%%%% script %%%%%%%%
maxpoint(3);

%%%%%%%% functions %%%%%%%
function maxpoint (p)
printf("point= %d",p);
endfunction

error: 'maxpoint' undefined near line 2 column 1


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave



1;  % this makes it a script

%%%%%%%% functions %%%%%%%
function maxpoint (p)
printf("point= %d \n",p);
endfunction

%%%%%%%%%% script %%%%%%%%
maxpoint(3)

--
DASCertificate for 206392


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Placing Functions Inline to Script

Fritz Sonnichsen
OK guys. That worked---peculiar syntax (;->

Thanks
Fritz

On 6/8/2017 11:26 AM, Doug Stewart wrote:


On Thu, Jun 8, 2017 at 10:03 AM, Fritz Sonnichsen <[hidden email]> wrote:
I am a new octave user (matlab refugee) and trying to get through a few very basic problems to get going. I am trying to define functions within a script as shown in the code below. The script fails claiming it cannot find the function. The only workaround is to make the main script a function as well but this has some repercussions regarding preserving variables. Short of having separate files running around with my functions is there a way to help the script "see" the function definition?

Thanks
Fritz

%%%%%%%%%% script %%%%%%%%
maxpoint(3);

%%%%%%%% functions %%%%%%%
function maxpoint (p)
printf("point= %d",p);
endfunction

error: 'maxpoint' undefined near line 2 column 1


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave



1;  % this makes it a script

%%%%%%%% functions %%%%%%%
function maxpoint (p)
printf("point= %d \n",p);
endfunction

%%%%%%%%%% script %%%%%%%%
maxpoint(3)

--
DASCertificate for 206392



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Placing Functions Inline to Script

NJank
In reply to this post by Doug Stewart-4
On Thu, Jun 8, 2017 at 11:26 AM, Doug Stewart <[hidden email]> wrote:


On Thu, Jun 8, 2017 at 10:03 AM, Fritz Sonnichsen <[hidden email]> wrote:
I am a new octave user (matlab refugee) and trying to get through a few very basic problems to get going. I am trying to define functions within a script as shown in the code below. The script fails claiming it cannot find the function. The only workaround is to make the main script a function as well but this has some repercussions regarding preserving variables. Short of having separate files running around with my functions is there a way to help the script "see" the function definition?

Thanks
Fritz

%%%%%%%%%% script %%%%%%%%
maxpoint(3);

%%%%%%%% functions %%%%%%%
function maxpoint (p)
printf("point= %d",p);
endfunction

error: 'maxpoint' undefined near line 2 column 1


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave



1;  % this makes it a script

%%%%%%%% functions %%%%%%%
function maxpoint (p)
printf("point= %d \n",p);
endfunction

%%%%%%%%%% script %%%%%%%%
maxpoint(3)



To explain a bit:  as you know there are scripts and function files. If the first executable line in an m-file is a function declaration, Octave will treat the file as a function that can then be called as a function in other scripts or at the command line.  The general convention is one function per file. The file can have other subfunctions listed after the main fucntion that will only visible to the main function.  Multiple files really aren't a problem unless you have particular concerns.

Scripts are just lists of commands that will be executed in the order they appear in the m-file.  Octave will treat it as a script if the first line is not a function definition, hence Doug put a '1;'  as the first line. Functions can then be defined inline in the script file, but the function must be defined before it is called (hence Julien's point of having the maxpoint(3) line after the function maxpoint definition)

Note that there have been some recent changes in Matlab about handling functions in script files that may cause some of this to change in the future, however the above info should cover more of your apparent needs for now.

Nick J.


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Placing Functions Inline to Script

Fritz Sonnichsen
Thanks Nick-this makes sense. You guys were prompt and very helpful. I used Matlab for about 10 years until I changed jobs and had long wanted to look into Octave so here I am. It looks good-I am able to run a lot of my old matlab scripts.

cheers
Fritz

On 6/8/2017 12:31 PM, Nicholas Jankowski wrote:
On Thu, Jun 8, 2017 at 11:26 AM, Doug Stewart <[hidden email]> wrote:


On Thu, Jun 8, 2017 at 10:03 AM, Fritz Sonnichsen <[hidden email]> wrote:
I am a new octave user (matlab refugee) and trying to get through a few very basic problems to get going. I am trying to define functions within a script as shown in the code below. The script fails claiming it cannot find the function. The only workaround is to make the main script a function as well but this has some repercussions regarding preserving variables. Short of having separate files running around with my functions is there a way to help the script "see" the function definition?

Thanks
Fritz

%%%%%%%%%% script %%%%%%%%
maxpoint(3);

%%%%%%%% functions %%%%%%%
function maxpoint (p)
printf("point= %d",p);
endfunction

error: 'maxpoint' undefined near line 2 column 1


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave



1;  % this makes it a script

%%%%%%%% functions %%%%%%%
function maxpoint (p)
printf("point= %d \n",p);
endfunction

%%%%%%%%%% script %%%%%%%%
maxpoint(3)



To explain a bit:  as you know there are scripts and function files. If the first executable line in an m-file is a function declaration, Octave will treat the file as a function that can then be called as a function in other scripts or at the command line.  The general convention is one function per file. The file can have other subfunctions listed after the main fucntion that will only visible to the main function.  Multiple files really aren't a problem unless you have particular concerns.

Scripts are just lists of commands that will be executed in the order they appear in the m-file.  Octave will treat it as a script if the first line is not a function definition, hence Doug put a '1;'  as the first line. Functions can then be defined inline in the script file, but the function must be defined before it is called (hence Julien's point of having the maxpoint(3) line after the function maxpoint definition)

Note that there have been some recent changes in Matlab about handling functions in script files that may cause some of this to change in the future, however the above info should cover more of your apparent needs for now.

Nick J.



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Placing Functions Inline to Script

NJank
On Thu, Jun 8, 2017 at 12:35 PM, Fritz Sonnichsen <[hidden email]> wrote:
> Thanks Nick-this makes sense. You guys were prompt and very helpful. I used
> Matlab for about 10 years until I changed jobs and had long wanted to look
> into Octave so here I am. It looks good-I am able to run a lot of my old
> matlab scripts.
>
> cheers
> Fritz

No problem, let us know if you find things that don't work.  We may
know about some issues, but it's always worth hearing about any
potential incompatibilities that may be fixable.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave