1

I would like to have a file with function AND with little code that calls that function, for example:

function foo()
   ...
end

foo()

I would like to save such code to file (one file) named "everything.m", and then type in "everything" in Matlab/Octave console my function foo would be defined AND called.

greenoldman
  • 16,895
  • 26
  • 119
  • 185
  • 1
    What's wrong with two files? Or do you want to have many functions in a single file? – Dan Apr 10 '13 at 09:57
  • 2
    Dan is right, you can save multiple functions in a single file. But remember you cannot mix scripts and functions. So in your case, in order to call foo() you probably need to write another function otherwise the matlab compiler will complain. – entropiece Apr 10 '13 at 09:59
  • @Dan, I want to have many functions in one file plus a call to one of them. – greenoldman Apr 10 '13 at 10:19
  • @Entropiece, thank you, can you post this as an answer, so I could accept it? – greenoldman Apr 10 '13 at 10:22
  • Seems like a case for [classes](http://www.mathworks.nl/help/matlab/matlab_oop/classes-in-the-matlab-language.html) :) – Rody Oldenhuis Apr 10 '13 at 10:25
  • @Entropiece: Please post the solution as an answer so that it may be accepted and may hence benefit the community. – Roney Michael Apr 10 '13 at 10:31

3 Answers3

2

The common way to do this sort of thing is to have a script called everything.m

% everything.m
foo();

and one file for each function you want to be able to use:

% foo.m
function foo()
   ...
end

Executing everything.m then calls all the functions you want to call (plus any additional tasks you might want to do at that stage).

Having all your functions on the Matlab path, means they are "defined" (accessible from the Matlab command prompt or inside other functions). This is "the Matlab way" :)

Note that it is possible to have more than one function per file,

% bar.m 

function bar()
    otherFunction();  % WORKS OK 
end

function otherFunction()
    ...
end

only the topmost function will be visible in the Matlab command prompt:

>> bar 
>> % no error
>> 
>> otherFunction
??? Undefined function or variable 'otherFunction'.
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
1

In Matlab you cannot mix scripts and functions. So in your case, in order to call foo() you need to write another function otherwise the matlab compiler will complain.

So everything.m should have a function called everything, where you can call foo() from

There is a similar question here

MATLAB script code and function code in the same file?

Community
  • 1
  • 1
entropiece
  • 389
  • 5
  • 10
0

I think you can write the code in a script and then save it with the same name of the function. For example if your function is foo, than when you save the file, its name has to be : foo.m . Calling foo from your command line executes the function. If you want to call the function only without input arguments, you have to define all the variables you need between the keywords "function" and "end". Also you can call the function successfully only if your current folder is the one where you saved the function. If you would call that function from another directory, you'll get an error. To make the function work "globally" you should add it to the "matlab path". Hope this helps you a bit.

Eugenio
  • 276
  • 1
  • 5