MATLAB script code and function code in the same file?

Possible duplicate:
In MATLAB, can I have a script and function definition in the same file?

Can I have code and MATLAB script code in the same file?

%% SAVED IN FILE myfunc.m (otherwise fail)
function [out1] = myfunc( x )
out1 = sqrt( 1 + (cos(x))^2 );
end

%%
%OTHER CRAP
y = 1:10
% use myfunc

This does not seem to work, even with the keyword end. Is this type of thing allowed or do I always need to have the EACH function in my own properly named file?

I'm sure I saw functions and code that use these functions in one file a couple of years ago.

+3
source share
2 answers

m- , . . , , .

:

_: myScript.m

function [] = myScript()
 y = 1:10;
 out1 = myfunc(y);
end

function [out1] = myfunc( x )
 out1 = sqrt( 1 + (cos(x))^2 );
end

F5, matlab myScript

+9

rossb83 answer , , , :

function sum = function myMath(a, b)
    foo = a + b;
    bar = mySubFunc(foo);
end

function bar = mySubFunc(foo)
    bar = foo^2;
end
+3

All Articles