A suggestion about the code

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

A suggestion about the code

Criptos
Hi!
I have programmed in Fortran and in Matlab (and obviously in Octave) and
I thought that there could be a small change in the way this programme
uses the functions. In Matlab (and in Octave too) functions must be
defined in a separate script, which can be a problem if you use many
functions and you save them in different folders. But Fortran has a
different problem: you have to define the functions in the same script
in which you are going to use them.
With these things in mind I came up with a simple solution that could be
applied to Octave: make possible for the user to declare the function in
the script in which it is going to be used and not necessarily to be in
a different script.
It is my suggestion for improving this project a bit.

Thanks for your attention

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

Re: A suggestion about the code

nrjank

What you're referring to, sub functions in scripts, has been the subject of much debate in the MATLAB community. For whatever reason, they have elected to disallow functions in scripts.

While Octave sets matlab compatibility as a goal, they do allow subfunctions to be defined in the scripts that use them. I found out the hard way that this is not MATLAB compatible, as I do like the convention over a mess of separate files.

I also found that for no discernable reason, octave's implementation of this seems not perfect (surely the errors weren't my fault :) ). I've had scripts unable to find functions for no good reason until closing and restarting Octave, at which point it would be fine. Sometimes adding a single commented line to a script would render a subfunction 'unfindable' and deleting it would fix things...

I never got around to putting a simple test together to isolate the problem, I wound up just putting them in separate files for MATLAB compatibility.  maybe i'll try to get back to it to see what the problem was and of its bug report worthy.

Nick j.

On May 22, 2015 5:50 PM, "Criptos" <[hidden email]> wrote:
Hi!
I have programmed in Fortran and in Matlab (and obviously in Octave) and
I thought that there could be a small change in the way this programme
uses the functions. In Matlab (and in Octave too) functions must be
defined in a separate script, which can be a problem if you use many
functions and you save them in different folders. But Fortran has a
different problem: you have to define the functions in the same script
in which you are going to use them.
With these things in mind I came up with a simple solution that could be
applied to Octave: make possible for the user to declare the function in
the script in which it is going to be used and not necessarily to be in
a different script.
It is my suggestion for improving this project a bit.

Thanks for your attention

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

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

Re: A suggestion about the code

Jordi Gutiérrez Hermoso-2
On Fri, 2015-05-22 at 19:44 -0400, Nicholas Jankowski wrote:
> I also found that for no discernable reason, octave's implementation of
> this seems not perfect (surely the errors weren't my fault :) ). I've had
> scripts unable to find functions for no good reason until closing and
> restarting Octave, at which point it would be fine.

The explanation is as follows: Octave allows files to be parsed in
script mode or function mode. Function files are those whose first
statement is "function". A script file is one whose first statement is
anything other than "function".

Script files have "side effects", so to speak. One of the side effects
of a script file is to define a new function. Thus if you define a
function in script mode, then the script has to be evaluated each time
for the changes to that function to take effect.

For a function defined in function mode, Octave always checks to see
if the file has changed without you needing to evaluate it.

This is also explained in the manual,

   https://www.gnu.org/software/octave/doc/interpreter/Script-Files.html

HTH,
- Jordi G. H.



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

Re: A suggestion about the code

nrjank


On May 23, 2015 10:47 AM, "Jordi Gutiérrez Hermoso" <[hidden email]> wrote:
>
> On Fri, 2015-05-22 at 19:44 -0400, Nicholas Jankowski wrote:
> > I also found that for no discernable reason, octave's implementation of
> > this seems not perfect (surely the errors weren't my fault :) ). I've had
> > scripts unable to find functions for no good reason until closing and
> > restarting Octave, at which point it would be fine.
>
> The explanation is as follows: Octave allows files to be parsed in
> script mode or function mode. Function files are those whose first
> statement is "function". A script file is one whose first statement is
> anything other than "function".
>
> Script files have "side effects", so to speak. One of the side effects
> of a script file is to define a new function. Thus if you define a
> function in script mode, then the script has to be evaluated each time
> for the changes to that function to take effect.
>
> For a function defined in function mode, Octave always checks to see
> if the file has changed without you needing to evaluate it.
>
> This is also explained in the manual,
>
>    https://www.gnu.org/software/octave/doc/interpreter/Script-Files.html
>
> HTH,
> - Jordi G. H.
>
>

Just tried this again. Everything works now but, and this didn't make sense until I really thought of the script as a script, only if you define the function ahead of the first call. Used to programming where I can pile all my functions at the end and order doesn't matter. That would work fine if I test-ran the script while editing, and didn't clear memory between runs. Hence I thought it would sometimes work and sometimes not.

So, throwing all my sub-functions in a section at the top of the script, usually after a clear so that 'function' isn't the first line, generally works fine. Makes for a cleaner workspace as long as I don't need ML compatibility.

NickJ


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