Got Questions? Get Answers.
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

Thread Subject:
functions

Subject: functions

From: Jenny Paul

Date: 20 Oct, 2010 17:11:05

Message: 1 of 7

I'm new to MATLAB... I do not understand how to write a function and then use it in a main program... PLEASE HELP!!

I know that a function is written as a .m file (such as, 'func.m')... but how do I get the main program to call the self written function??

Thank you in advance for any input you can provide

Subject: functions

From: Sean

Date: 20 Oct, 2010 17:23:06

Message: 2 of 7

"Jenny Paul" <jenny.paul08@gmail.com> wrote in message <i9n7r9$7dd$1@fred.mathworks.com>...
> I'm new to MATLAB... I do not understand how to write a function and then use it in a main program... PLEASE HELP!!
>
> I know that a function is written as a .m file (such as, 'func.m')... but how do I get the main program to call the self written function??
>
> Thank you in advance for any input you can provide

read the information at:
doc function

function [out1 out2] = func(in1,in2)
  out1 = in1.*in2;
  out2 = norm(out1);
end

Subject: functions

From: Jenny Paul

Date: 20 Oct, 2010 18:03:04

Message: 3 of 7

My function is written as follows::
---------------------------------------------------------------------------
%interp.m
function y=interp(T,T1,T2,y1,y2)
    y=y1+(T-T1)*(y2-y1)/(T2-T1);
end
----------------------------------------------------------------------------
My problem is:

The main program is not using the function property and I believe it is due to the code improperly calling the function... what is the correct method of calling this function to another program???

Subject: functions

From: Jan Simon

Date: 20 Oct, 2010 18:05:04

Message: 4 of 7

Dear Sean,

imagine that all command you can call in Matlab are M-functions (although some are "built-in", such that you cannot open them in the editor).
A good point to start is reading e.g. the "mean.m" function:
  edit mean
Then read the help of mean:
  help mean
Type in the example shown in the help text.

Welcome to Matlab, Jan

Subject: functions

From: Sean

Date: 20 Oct, 2010 18:08:03

Message: 5 of 7

"Jenny Paul" <jenny.paul08@gmail.com> wrote in message <i9naso$ol2$1@fred.mathworks.com>...
> My function is written as follows::
> ---------------------------------------------------------------------------
> %interp.m
> function y=interp(T,T1,T2,y1,y2)
> y=y1+(T-T1)*(y2-y1)/(T2-T1);
> end
> ----------------------------------------------------------------------------
> My problem is:
>
> The main program is not using the function property and I believe it is due to the code improperly calling the function... what is the correct method of calling this function to another program???

y = interp(T,T1,T2,y1,y2);

Subject: functions

From: Jan Simon

Date: 20 Oct, 2010 18:18:04

Message: 6 of 7

Dear Jenny,

> %interp.m
> function y=interp(T,T1,T2,y1,y2)
> y=y1+(T-T1)*(y2-y1)/(T2-T1);
> end
> The main program is not using the function property and I believe it is due to the code improperly calling the function.

Show us your calling command and post the error message or describe the difference between the results and your expectations. Please add the sizes and type of the used variables.

Kind regards, Jan

Subject: functions

From: dpb

Date: 20 Oct, 2010 21:55:34

Message: 7 of 7

Jenny Paul wrote:
> I'm new to MATLAB... I do not understand how to write a function and
> then use it in a main program... PLEASE HELP!!
>
> I know that a function is written as a .m file (such as, 'func.m')...
> but how do I get the main program to call the self written function??

Start w/ "Getting Started" section on Programming. It leads you thru
the process of an initial m-file exercise.

The short story is, once you name the m-file the same as the function,
you simply use the name as in any other function in Matlab.

You need the function file to be in a directory in the Matlab search
path, of course, in order to be found.

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us