Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How do write a function within the same main code

Asked by Watheq Elias on 8 Apr 2011

Hello everybody,

I want to know if it is possible to write a function within the same main code rather than write it in separate M file.

Thanks in advance for any help.

0 Comments

Watheq Elias

Products

No products are associated with this question.

2 Answers

Answer by Matt Fig on 8 Apr 2011
Accepted answer

Put all of this into one M-file. Then call it like this: get_sub(5)

function [OUT] = get_sub(A)
OUT = subfun(A);  % Call the subfunction.
function [OUT_SUB] = subfun(B)
OUT_SUB = B.^2; 

1 Comment

Jan Simon on 28 Jun 2012

accepted by JSimon

Matt Fig
Answer by Paulo Silva on 8 Apr 2011

yes with subfunctions

1 Comment

Matt Tearle on 9 Apr 2011

Or nested functions

Paulo Silva

Contact us