Related to function call

Hello..I seek help in writing MATLAB code. I have written a code for some operation and I am getting the result. But I want to call this functio code in my main code. I know how to do function call but i don't know which syntax to use at the end of function code so that I can pass the final value to my main code. For example, I have a code for convolution "my_conv.m" without using builin conv(x,h). In my main code I used the syntax "my_conv(..,..);" for function call. Which syntax I need to use at the end of "my_conv" for passing the result to main code? Similar to this I am using many other functions for different operations. Thank you in advance.

 Accepted Answer

Have you looked at the help of function? It's all explained in there.
You declare which variable you want to return in the first line of your code:
function returnvalue = myfunc(someinput, someotherinput)
%output of function is returnvalue
%...
end
and you call it:
value = myfunc(a, b)
See the help linked above to see how to return more than one output from a function.

6 Comments

Thank you for such a quick reply. In 'my_conv' code output variable is 'y'.I have used the same syntax that you have given for declaring the function and calling the function as well. Is it enough if we end the function using "end"? Or is there any syntax to use further to pass the value of 'y' to my main code? I am getting the result but I am getting an error also. The error is, "Undefined function or method 'Y' for input arguments of type 'double'." What does it mean?
Guillaume
Guillaume on 14 Sep 2015
Edited: Guillaume on 14 Sep 2015
Yes you don't need to do anything more in my_conv.m. The problem is most likely in the way you're calling the function. Can you show the exact code you're using?
Your my_conv.m file should have:
function y = my_conv(something, maybesomethingelse)
%short explanation of my_conv
%... some code
y = .... %assignment to y somewhere in the code
% ... maybe some more code
end
In your main code m file:
something = my_conv(somethingelse, maybesomeotherthing)
%... use something
Yeah sure. Below are the my_conv code and a small part of my main code where I call the function.
function y = my_conv(x,h);
% this is a code for convolution without using the inbuilt function.i
% wrote this code as a user defined function for convolution.
m=length(x);
n=length(h);
X=[x,zeros(1,n)]; % zero padding of both x[n] and h[n]
H=[h,zeros(1,m)]; % if size of x[n] ? size of h[n]
M=m+n-1;
for i=1:M
y(i)=0;
for j=1:1:M;
if(i-j+1>0)
y(i)=Y(i)+X(j)*H(i-j+1);
else
end
end
end
%% this is a small part of my main code where i do call the function my_conv insted of inbuilt function conv
d=[-1 -2 0 2 1]*(1/8);%Derivative component
ecg_d=my_conv(ecg_h,d);
ecg_d = ecg_d/max(ecg_d);% Derivated Signal
Hope this will help you to solve my doubt.
Hi.. Do you say every function syntax has end syntax as its pair? Like "for-end" pair, "if-end" pair, "function-end" is also a valid pair? every "function" we declare has to have an "end" command?
In .m files that start with "function", the rule is that you have to do it the same way every time: you can have functions that have matching "end", or you can have functions that do not have matching "end", but you cannot have both types in the same file. Having a matching "end" means something slightly different: it can be optimized more, but you cannot create new variables dynamically in the function by using eval() or related routines or by using load() without an output parameter.
OK...thank you..

Sign in to comment.

More Answers (0)

Asked:

on 14 Sep 2015

Commented:

on 16 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!