You are here: Home → Forum Home → The Mac Observer Forums → Design & Create → Thread
Matlab/Octave Function declaration?
-
Has anyone done much programming in Matlab (more specifically Octave, but they should be more or less identical)
how the hell do you declare multiple functions in the same file!
function f () g () end
function g () h () end
function h () nargin == 1 || error ("nargin != 1"); endPut this in a file named f.m It compiles f, but then sees g as unknown. you can’t put g in front of f because then it isn’t named properly for the file, and if you put the end for function f at the end of the entire file (as I thought you were supposed) octave spits back an error saying you can’t declare a function inside a function (kind of makes sense). I can’t even find a good example program with two functions in one file. All of the Octave builtin examples contain one function per file! (at least all the ones I looked at.)
grrr, I want to go back to IDL.
little help?
Signature
All statements are to be taken with a grain of salt
-
On 2002-02-25 03:46, EDGar wrote:
Has anyone done much programming in Matlab (more specifically Octave, but they should be more or less identical)how the hell do you declare multiple functions in the same file!
function f () g () end
function g () h () end
function h () nargin == 1 || error ("nargin != 1"); endPut this in a file named f.m It compiles f, but then sees g as unknown. you can’t put g in front of f because then it isn’t named properly for the file, and if you put the end for function f at the end of the entire file (as I thought you were supposed) octave spits back an error saying you can’t declare a function inside a function (kind of makes sense). I can’t even find a good example program with two functions in one file. All of the Octave builtin examples contain one function per file! (at least all the ones I looked at.)
EDGar, I’m sure there is a reason in your specific code, but why can’t you simply do somethin like this:
function f () h () end
function h () nargin == 1 || error ("nargin != 1"); endand forget the “function g ()” altogether? Or is the “function g ()” there for a specific reason?
grrr, I want to go back to IDL.
I can certainly understand that ...
Signature
Work is the curse of the drinking classes.
- Oscar Wilde -
no, g() isn’t there for a particular reason, but it also shouldn’t matter. I grabbed that example straight out of a help manual, and it works fine at the command line because it doesn’t actually call f() at first it just declares it, then by the time I call it I have also declared g() and h() so it has no problem. So… maybe what I am really after is a way to compile a file before running it.
<smacks forehead> I’ll RTFM again. If anyone knows how off the top of their head let me know. In Matlab I think there is a GUI option compile. is there a command line option to compile too?
something like IDL’s .compile functionality?
Signature
All statements are to be taken with a grain of salt
-
Glad to help. I know nothing about Matlab, but I know a lot of languages, and I also know that getting someone to talk out loud is often the best; the person will figure it out on his own. “Self-debugging”, a friend calls it.
Signature
Work is the curse of the drinking classes.
- Oscar Wilde

