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

accepted by JSimon

Answer by Paulo Silva
on 8 Apr 2011

yes with subfunctions

  1 Comment

Or nested functions

Join the 15-year community celebration.

Play games and win prizes!

Learn more
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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!