MATLAB Answers

Watheq Elias

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.



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

Answer by Paulo Silva
on 8 Apr 2011

yes with subfunctions

  1 Comment

Matt Tearle
on 9 Apr 2011

Or nested functions

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today