help-octave
[Top][All Lists]
Advanced

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

Re: function help


From: Ben Abbott
Subject: Re: function help
Date: Thu, 23 Feb 2012 18:46:28 -0500

On Feb 23, 2012, at 6:37 PM, Tom Bohdan wrote:

> --- On Fri, 24/2/12, Ben Abbott <address@hidden> wrote:
> 
>> From: Ben Abbott <address@hidden>
>> Subject: Re: function help
>> To: "Tom Bohdan" <address@hidden>
>> Cc: "help-octave Octave" <address@hidden>
>> Received: Friday, 24 February, 2012, 10:12 AM
>> 
>> On Feb 23, 2012, at 5:56 PM, Tom Bohdan wrote:
>> 
>> > --- On Fri, 24/2/12, Ben Abbott <address@hidden> wrote:
>> > 
>> >> On Feb 23, 2012, at 4:26 AM, Tom Bohdan <address@hidden> wrote:
>> >> 
>> >>> I have had no success with finding why the following doesn't work. I 
>> >>> have tried many examples in Octave's Help and they don't work either.
>> >>> 
>> >>> Why is it that if you type each line into the command line then the 
>> >>> following works:
>> >>> 
>> >>> function total=addr(x,y)
>> >>> total=x+y;
>> >>> endfunction
>> >>> 
>> >>> add(5,4)
>> >>> 
>> >>> >ans=9
>> >>> 
>> >>> but if i put the above lines into a m-file then the interpreter spits 
>> >>> out x undefined etc.
>> >>> 
>> >>> This happens for all the functions i have found in the LSODE examples 
>> >>> etc. If you type them one by one at the command line it works but not in 
>> >>> a script file. What am i missing ???
>> >>> 
>> >>> regards
>> >>> Tom
>> >> 
>> >> Did you intend "add()" or "addr()" ?
>> >> 
>> >> Ben
>> > 
>> > thanks for your prompt reply. 
>> > Another person suggested that it was because there may be a function 
>> > called add() that may be messing it up but i retried the following and got 
>> > the same problems:
>> > 
>> > function total=exxample(x,y)
>> > total=x+y;
>> > endfunction
>> > exxample(9,8)
>> > 
>> > At home i use Ubuntu 10.10 LTS which has octave 2.3.3. I typically use QT 
>> > octave but i tried it on the command line of Octave itself too.
>> > 
>> > At work i use Windows Xp and QT Octave and Octave 2.3.4. Again similar 
>> > issues BUT i did have some success.
>> > 
>> > if i hit Run within the editor of QT octave on the Windows version only 
>> > the script worked !
>> > 
>> > If i run it by the Command line in the dos window it failed exactly like 
>> > the Linux versions with undefined x in row2, col7 or something like that.
>> > 
>> > If its typed line -by-line into the Command line it works too. It must be 
>> > a bug?
>> 
>> When you write "run it by the Command line", but exactly did you do? How is 
>> it different than typing in at the Command line ?
>> 
>> Ben
> 
> by running it at the command line:
> 
> (A) I typed each line of the file at the octave prompt and hit enter at the 
> end of each line. After typing in the function, i then wrote exxample(8,9) 
> and hit enter to get ans=17
> 
> (B) I saved the function and last line exxample(8,9) into a M-file called 
> exxample.m. i then typed "exxample" at the Octave prompt. It then spits out x 
> undefined, etc

The exxample.m file isn't a script file. It is a function file.

When you enter "exxample", you've call a function, but didn't provide the 
inputs (x,y). The first time one of the inputs is needed you're get an error "x 
undefined".

If' you'd like to make exxample.m a script file, the first executable command 
must be something other that "function ...". For example, 

------- begin exxample.m -------
1; 
function total=exxample(x,y)
  total=x+y;
endfunction
exxample(9,8)
-------- end exxample.m --------

Ben




reply via email to

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