Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How should I define a function in Matlab code?

Status
Not open for further replies.

saurabhtaksali

Newbie level 6
Joined
Feb 18, 2009
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,364
hello

i am working on matlab and i have some problem with defining functions in the code .. there is a error coming which says that you cant define a function in prompt or in scripts .. can anyone tell me wat does it mean ... or how should i define the function ..
i ll be grateful
 

matlab define function

It sounds like you are working from the command line. You can't define a function from there. To create a function, you have to make an "m file". M files are what matlab uses.

So to create a function, here's a procedure. Select file -> new -> M-File.

This will bring up a blank document. The first line you type is to define your function, along with any inputs and outputs. I'll write a simple one here to convert from linear to dB. the name of the function will be todB, with input X and output Y.

function [Y] = todB(X)
Y = 10*log10(X);


Now save the file and go back to the command line and type "test = todB(130)"

Now test = 21.1394, which is 130 in dB.

So in summary, a function has to be saved in an m-file. You cannot define it from the command line. The first line of the file HAS to be the line:
function [out1, out2, ...] = func_Name (input1, intput2, ...)

Another thing about functions is that they operate in their own workspace. Thus, when a function is running, it can only "see" the variables that were explicitly defined as inputs. Also, after the function is done running, all the variables that may have been defined inside the function disappear, unless they are explicitly defined as outputs.
 

defining a function in matlab

very good AndyECE
 

how to define a function in matlab

ya actually it helps thanks a lot
 

multiple functions in one m file

hello

can we define more than one function in a single m file .
 

define a function in matlab

A single M-file may hold more than one function definition. A new function header line in a

file ends the primary function and starts a new subfunction. As a silly example, consider

function [x1,x2] = quadform(a,b,c)

d = discrim(a,b,c);

x1 = (-b + d) / (2*a);

x2 = (-b - d) / (2*a);

function D = discrim(a,b,c)

D = sqrt(bˆ2 - 4*a*c);
 

matlab function definition

In response to the message:

===============================
hi well thanks a lot for your help ... 1 more thing i want to ask is
if we define a signal as a global signal in a function then we can
use it other functions also right?
i may need some more help on matlab regarding my project .. it wud be
great if you dont mind me asking you...
===============================


Technically, yes you can. However, you shouldn't use global variables. It is a bad programming practice. I really recommend that you get into the habit of coding well. It will save you a lot of frustration in the future.

Let's keep posting in this this thread. This is a good discussion for other people who are new to MATLAB. If you post code, I'll be glad to take a look at it.
 

define function matlab

u've to create mfile
 

matlab defining functions

no problem dear
 

matlab multiple functions in one file

hi,
if u define two functions in a single .m file then how do you save the name of the file.

function name 1 or function name 2

bcos usually we do save the file name in the name of function name

thats y i got the doubt.
 

more than one function in one m file matlab

The name of the file is how Matlab recognizes it.

In the above example for quadForm, you cannot access the secondary function 'discrim' directly. In fact, if the file were saved as something else, say "qForm", then you can't even access quadForm directly. It goes back to the idea of a workspace. You would type in "qForm(a,b,c)"

Matlab now opens the file "qForm" and reads it from top to bottom. The first line is a function definition and so Matlab says "aha! a function!" and begins a new work space. This work space involves only the variables a, b, and c. It also includes any other functions defined inside the m file.

To review, if Aya2002's file were saved as qForm, then:

>>quadForm(a, b, c) ===> ERROR
>>discrim(a, b, c) =====> ERROR
>>qForm(a, b, c) =====> WORKS

So in the case of multiple functions defined in an m file, the first function is the one executed. Once you are inside the first function, you can call another function. -BUT- once you call that second function, then yet ANOTHER workspace is started that lives inside the first. In this nested workspace, once again the only variables you can see are the ones explicitly passed to the inner function.
 

defining functions within program in matlab

so how should we save that file with two function or shud we save them in different files .. one more thing i have written a function but while using it it says that it has dimension not matching error in the last line .. i have put the code here plz see if neone can help

function [channel ,rx] = add_channel_effect(channel,rx,signal_sequence)
global signal;
channel.attenuation.d = 1 / (channel.attenuation.distance ^ 2);
nr_of_blocks = ceil(size(signal_sequence,2) /channel.attenuation.block_length);
h_block = (randn(nr_of_blocks,1) + j * randn(nr_of_blocks,1)) * channel.attenuation.d;
h = reshape((h_block *ones(1, channel.attenuation.block_length))', 1,channel.attenuation.block_length * nr_of_blocks);
channel.attenuation.h = h(1:(size(signal_sequence,2)));
[channel.attenuation.phi, channel.attenuation.h_mag] =cart2pol(real(channel.attenuation.h),imag(channel.attenuation.h));
channel.attenuation.phi = -channel.attenuation.phi;
% Noise (AVGN)
S = mean(abs(signal_sequence).^2);
SNR_linear = 10^(channel.noise.SNR/10);
%SNR = a^2/(2*sigma^2)
channel.noise.sigma = sqrt(S / (2 * SNR_linear));
noise_vector = (randn(size(signal_sequence)) +j * randn(size(signal_sequence))) * channel.noise.sigma;
rx.received_signal = signal_sequence * channel.attenuation.h + noise_vector;
 

matlab multiple function inside a file

If you have two functions you want to be able to access from the command line, then you have to save them in different m-files. I poked around matlab help and have confirmed that there seems to be no way to get around it

Looking at your code, the first thing I see is that you are trying to multiply two vectors in the line

rx.received_signal = signal_sequence * channel.attenuation.h + noise_vector;

there are two problems I see:

from what I can see,

channel.attenuation.h is a "row vector". The reshape command will make size(channel.attenuation.h) = [1 x channel.attenuation.block_length * nr_of_blocks]

the dimensions of signal_sequence is not well defined, because you have it as an input. Please clarify the size of each input, it will make debugging easier

noise_vector is a SQUARE matrix, because if you only pass a single argument to 'randn', it assumes you want a square matrix.

So the way I see if you are trying to do one of two things:
THING 1:
If you want rx.received_signal to be a square matrix, then signal_sequence has to be a "column vector", that is size(signal_sequence) = [Nx1], where N = channel.attenuation.block_length * nr_of_blocks (the same number as channel.attenuation.h)

The '*' operator will try to apply linear algebra rules, which means that you cannot multiple two tow vectors or two coulmn vectors. A column vector(signal_sequence) times a row vector (channel.attenuation.h ) gives you a matrix. Now you can add noise_vector, which is a matrix of the same size.

THING 2:

If you want rx.received_signal to be a sequence (that is a row vector or column vector), then you have to change two things:

change noise_vector to:

noise_vector = (randn(1, size(signal_sequence)) +j * randn(1, size(signal_sequence))) * channel.noise.sigma;

Now size(noise_vector) = size(channel.attenuation.h)

The next thing to do is change the '*' between signal_sequence and channel.attenuation.h to '.*'

* and .* are different operations in matlab. '.*' tells matlab to multiply element-wise. This will make rx.received_signal a sequence instead of a matrix.

------------------------

I can help clarify if you specify:
the size of each input
what the size of the output you are trying to get is
 

Re: matlab multiple function inside a file

code function in MATLAB
hhi everybody

i need to code a simple function in matlab and i have some problems
the function getting 4 variable and supose to plot 4 waves.
if the user press just 2 variables thus the function plots 2 waves.

i try to coded its like

function present_sig[x1,x2,x3,x4]
...

if nargin == 2

plot.......

elsif nargin ==4
plot.......

end

and the i try to operste the function from another m. file
the code is ;

persent_sig[x1,x2,x3,x4]
x1=...
x2=...
x3=...
x4=...

and from somehow it doesnt works good
i et error that thevariables undefined.....


i ill be very happy to any assist

thanks all
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top