Functions in Octave

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

Functions in Octave

fbarbuto
The way Octave works puzzles me sometimes.

The very simple, proof-of-concept testfun.m calls two functions and it works.

Source2.m also calls two functions, but doesn't work. I get the following error message:

error: 'BporLinhas' undefined near line 54 column 20
error: called from
    Source2 at line 54 column 18

The more I examine their structures, the less I understand why one code runs fine whereas the other doesn't.

Source2.m was ported from Matlab, where it does work.

What's the problem?

Fausto

testefun.m

Source2.m
Reply | Threaded
Open this post in threaded view
|

Re: Functions in Octave

James Sherman
On Thu, Aug 27, 2015 at 12:58 PM, fbarbuto <[hidden email]> wrote:

> The way Octave works puzzles me sometimes.
>
> The very simple, proof-of-concept testfun.m calls two functions and it
> works.
>
> Source2.m also calls two functions, but doesn't work. I get the following
> error message:
>
> *error: 'BporLinhas' undefined near line 54 column 20
> error: called from
>     Source2 at line 54 column 18
> *
> The more I examine their structures, the less I understand why one code runs
> fine whereas the other doesn't.
>
> Source2.m was ported from Matlab, where it /does/ work.
>
> What's the problem?
>
> Fausto
>
> testefun.m <http://octave.1599824.n4.nabble.com/file/n4672321/testefun.m>
>
> Source2.m <http://octave.1599824.n4.nabble.com/file/n4672321/Source2.m>
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Functions-in-Octave-tp4672321.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

I just tried your Source2.m file in Matlab, and I got the following error:

>> source2
Error: File: source2.m Line: 87 Column: 1
Function definition is misplaced or improperly nested.

Which I expected, since I didn't think that Matlab supported multiple
function definitions in the same file.  Are you sure that your
Source2.m, as written, works in Matlab?  If you write each of your
functions in separate files named BporLinhas.m and BporColunas.m and
then run your Source2.m function (in Octave), what happens then?  Make
sure that either your current directory is the same directory as the
three files, or that the path contains that directory.

Hope this helps,

James Sherman

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

Re: Functions in Octave

fbarbuto

Hi James,

As I wrote, Source2.m was ported from Matlab and I obviously had to
adequate it to Octave. Then several "end" commands in the original
Matlab code were replaced by "endfunction", "endfor" and "endif"
whenever and wherever required. But that was it. The logic of both
Matlab and Octave scripts is the very same however.

As expected, Source2.m works if I write BporLinhas.m and BporColunas.m
as separate files. But I don't want that. I want the functions to sit
inside Source2.m so that they can be readily visible/editable.

Lastly, the big question: testefun.m works and I hadn't to write its
two functions as separate files. Why?

Thanks for your reply.

Fausto




On Thursday, August 27, 2015 2:27 PM, James Sherman Jr. <[hidden email]> wrote:


On Thu, Aug 27, 2015 at 12:58 PM, fbarbuto <[hidden email]> wrote:

> The way Octave works puzzles me sometimes.
>
> The very simple, proof-of-concept testfun.m calls two functions and it
> works.
>
> Source2.m also calls two functions, but doesn't work. I get the following
> error message:
>
> *error: 'BporLinhas' undefined near line 54 column 20
> error: called from
>    Source2 at line 54 column 18
> *
> The more I examine their structures, the less I understand why one code runs
> fine whereas the other doesn't.
>
> Source2.m was ported from Matlab, where it /does/ work.
>
> What's the problem?
>
> Fausto
>
> testefun.m <http://octave.1599824.n4.nabble.com/file/n4672321/testefun.m>
>
> Source2.m <http://octave.1599824.n4.nabble.com/file/n4672321/Source2.m>
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Functions-in-Octave-tp4672321.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

I just tried your Source2.m file in Matlab, and I got the following error:

>> source2
Error: File: source2.m Line: 87 Column: 1
Function definition is misplaced or improperly nested.

Which I expected, since I didn't think that Matlab supported multiple
function definitions in the same file.  Are you sure that your
Source2.m, as written, works in Matlab?  If you write each of your
functions in separate files named BporLinhas.m and BporColunas.m and
then run your Source2.m function (in Octave), what happens then?  Make
sure that either your current directory is the same directory as the
three files, or that the path contains that directory.

Hope this helps,

James Sherman



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

Re: Functions in Octave

Mike Miller-4
On Thu, Aug 27, 2015 at 18:02:56 +0000, Fausto Arinos de A. Barbuto wrote:
> Lastly, the big question: testefun.m works and I hadn't to write its
> two functions as separate files. Why?

Because testefun calls "clear" rather than "clear all" :)

You are falling prey to bug #35881 [1]. The "clear all" command clears
the functions knowledge of its own subfunctions and they can't be
called.

[1]: https://savannah.gnu.org/bugs/?35881

--
mike

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

Re: Functions in Octave

fbarbuto

Hi Mike;

Thanks a lot. The code now works and a lesson was learnt. ;-)

Fausto





On Thursday, August 27, 2015 3:10 PM, Mike Miller <[hidden email]> wrote:


On Thu, Aug 27, 2015 at 18:02:56 +0000, Fausto Arinos de A. Barbuto wrote:
> Lastly, the big question: testefun.m works and I hadn't to write its
> two functions as separate files. Why?

Because testefun calls "clear" rather than "clear all" :)

You are falling prey to bug #35881 [1]. The "clear all" command clears
the functions knowledge of its own subfunctions and they can't be

called.

[1]:
https://savannah.gnu.org/bugs/?35881

--
mike




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

Re: Functions in Octave

nrjank
In reply to this post by fbarbuto
On Thu, Aug 27, 2015 at 2:02 PM, Fausto Arinos de A. Barbuto <[hidden email]> wrote:

Hi James,

As I wrote, Source2.m was ported from Matlab and I obviously had to
adequate it to Octave. Then several "end" commands in the original
Matlab code were replaced by "endfunction", "endfor" and "endif"
whenever and wherever required. But that was it. The logic of both
Matlab and Octave scripts is the very same however.

As expected, Source2.m works if I write BporLinhas.m and BporColunas.m
as separate files. But I don't want that. I want the functions to sit
inside Source2.m so that they can be readily visible/editable.

Lastly, the big question: testefun.m works and I hadn't to write its
two functions as separate files. Why?

Thanks for your reply.

Fausto




On Thursday, August 27, 2015 2:27 PM, James Sherman Jr. <[hidden email]> wrote:


On Thu, Aug 27, 2015 at 12:58 PM, fbarbuto <[hidden email]> wrote:

> The way Octave works puzzles me sometimes.
>
> The very simple, proof-of-concept testfun.m calls two functions and it
> works.
>
> Source2.m also calls two functions, but doesn't work. I get the following
> error message:
>
> *error: 'BporLinhas' undefined near line 54 column 20
> error: called from
>    Source2 at line 54 column 18
> *
> The more I examine their structures, the less I understand why one code runs
> fine whereas the other doesn't.
>
> Source2.m was ported from Matlab, where it /does/ work.
>
> What's the problem?
>
> Fausto
>
> testefun.m <http://octave.1599824.n4.nabble.com/file/n4672321/testefun.m>
>
> Source2.m <http://octave.1599824.n4.nabble.com/file/n4672321/Source2.m>
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Functions-in-Octave-tp4672321.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

I just tried your Source2.m file in Matlab, and I got the following error:

>> source2
Error: File: source2.m Line: 87 Column: 1
Function definition is misplaced or improperly nested.

Which I expected, since I didn't think that Matlab supported multiple
function definitions in the same file.  Are you sure that your
Source2.m, as written, works in Matlab?  If you write each of your
functions in separate files named BporLinhas.m and BporColunas.m and
then run your Source2.m function (in Octave), what happens then?  Make
sure that either your current directory is the same directory as the
three files, or that the path contains that directory.

Hope this helps,

James Sherman



Fausto:

just for reference, Octave is fine with using 'end' versus the more specific 'endfor', 'endif', etc. so there's no need to change those when you're porting code from ML to Octave. some prefer it as it can make code easier to follow.



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