[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 10:02:08 +0000 |
>>> 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, the 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. It isn't stressed that functions need to sit in its own
separate file of the same name, and perhaps it could be made clearer to stop
people stubbing their toe very early and walking away?
Damian Harty
Senior Research Fellow
Coventry University
+44(0)24 7688 8924
+44(0)7799 414832
- Re: function help, (continued)
- Re: function help, Ben Abbott, 2012/02/23
- Re: function help, damian.harty, 2012/02/23
- Re: function help, damian.harty, 2012/02/23
- Re: function help, Jordi GutiƩrrez Hermoso, 2012/02/23
- Re: function help, Ben Abbott, 2012/02/23
- Re: function help, Ben Abbott, 2012/02/23
- RE: function help,
Damian Harty <=
- Re: function help, Doug Stewart, 2012/02/24
- Re: function help, damian.harty, 2012/02/24
- Re: function help, Doug Stewart, 2012/02/24
- Re: function help, Doug Stewart, 2012/02/24
- Re: function help, Ben Abbott, 2012/02/24
- Re: function help, Doug Stewart, 2012/02/24
- Re: function help, Ben Abbott, 2012/02/25
- Re: function help, Doug Stewart, 2012/02/25
- Re: function help, John W. Eaton, 2012/02/25
- Re: function help, Ben Abbott, 2012/02/25