help-octave
[Top][All Lists]
Advanced

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

Re: function help


From: damian.harty
Subject: Re: function help
Date: Fri, 24 Feb 2012 02:14:15 -0800 (PST)

>>> Because by design all the additional function definitions in the .m 
>>> file are local and hidden to the outside world.
>> 
>> Well, that's the bit I'm not getting. If they are in the script file 
>> I'm working in, they don't get much more local than that, do they? I 
>> can understand why they would be invisible, say, from the command 
>> line despite being present in a script, but I can't understand why 
>> they aren't visible when they are /in this file/ and why instead they 
>> /have to be in a file with their own name/.
>
> Maybe you can attach the actual script so that others can attempt to
> duplicate what you are seeing ?

Ah, I did it via that "Nabble" thing and I see it deleted the formatted
parts from my earlier post...

The file called add.m contains:

function total=add(x,y)
  total=x+y)
endfunction

The file function_test.m contains:

answer=add(5,9)

This works as advertised.

If at this point I do "which add" it tells me it is "a function from the
file add.m" - so far, no surprises.

As I noted earlier, if I now do clear all and which add, there is no
existing function. Again, no surprises.

Now, as the original poster noted, if I do clear all, rename add.m to
banana.m and then type the following four lines in at the command line (as
suggested in the documentation):

function total=add(x,y)
  total=x+y)
endfunction
answer=add(5,9)

...then it also works as advertised. If at this juncture I do "which add" it
tells me it is a "command line function".

What doesn't work, and what the original poster found counter-intuitive is
that if you put the previous four lines in a file called eric.m then I get
an error saying
------------------------------------
Warning: function name `add' does not agree with function file name...
parse error near line 4...

  syntax error

>>>answer=add(5,9)
        ^
------------------------------------

Now, it might well be that this is explained somehow in the help, but as a
new starter I am simply commenting that I can't find a succinct explanation
and that, moreover, some examples in the documentation give some optimism
about grouping functions within a single source file that turns out to be
unfounded. I can't comment on whether the aspiration so to do is a good one
or a bad one, but I can assert that the confusion on the part of the
original poster is entirely understood by me. Both files are .m files, one
of them contains a function definition. I read the help documentation on
"functions and scripts" and it isn't stressed that each function needs to
sit in its own separate file of the same name. The command line behaviour is
confusing because there appears to be a "bucket" into which functions can be
placed, but this isn't so when running an m-file, as evidenced by my
(somewhat laboured) examples above.




Damian Harty
Senior Research Fellow
Coventry University
+44(0)24 7688 8924
+44(0)7799 414832



-----
Senior Research Fellow - Vehicle & System Dynamics
Coventry University
United Kingdom
--
View this message in context: 
http://octave.1599824.n4.nabble.com/function-help-tp4414098p4416727.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

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