Calling a function in MATLAB

972 views (last 30 days)
user06
user06 on 23 Feb 2015
Commented: Kebba Bah on 30 Dec 2019
How can I call a function written in an .m file?
  1 Comment
Kebba Bah
Kebba Bah on 30 Dec 2019
clc
clear
clear all
want_to_do='show';
tables='Yes';
save_results=0;
%%%%%%%%%%%%%%%%%%% new add %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Load Data %%%%%%%%%%%%%%%%%%%%
data=xlsread('C:\Users\DELL\Desktop\Files\Workr\DEA_prac\DEA_prac\K011.xls');
id=data(:,1); Year=data(:,2); Y=data(:,3);
X_H=data(:,6); X_K=data(:,7); X_L=data(:,5);
%%%%%%%%%%%%%%%%sum of Squares variables
Y_sq=sum(Y.^2);
X_Hsq=sum(X_H.^2);
X_Ksq=sum(X_K.^2);
X_Lsq=sum(X_L.^2);
%%%%%%%%%%%%%%%Normalizing variables Variable%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Y_norm=Y./Y_sq;
X_Hnorm=X_H./X_Hsq;
X_Knorm=X_K./X_Ksq;
X_Lnorm=X_L./X_Lsq;
%%%%%%%% Input and output vectors
input=[X_Hnorm, X_Knorm, X_Lnorm];
output=[Y_norm];
data1=[input,output];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=size(data,1);
t=(min(data(:,2)):1:max(data(:,2)))';
no_time=size(t,1);
N=size(data1,1);
Time=data(:,2);
% Output distance function estimation by DEA (O'Donnell,2011)
function [OTE,exitflag,count_OTE]=outputTE_DEA_unban(output,input,N,t,Time, 1)
exitflag=[];
options=optimset('Display', 'off');
J=size(output,2);
K=size(input,2);
OTE=zeros(N,1);
n_t=size(t,1);
for i=1:n_t%%%% t
Q=[];
X=[];
for j=1:N
if Time(j)==t(i)
Q=[Q; output(j,:)];
X=[X; input(j,:)];
end
end
eval(['Q',num2str(i),'=Q;']);
eval(['X',num2str(i),'=X;']);
end
count_OTE=0;
for i=1:N
for j=1:n_t%%%%% t
if t(j)==Time(i)
eval(['Qm=Q',num2str(j),';'])
eval(['Xm=X',num2str(j),';'])
end
end
Qm=Qm';
Xm=Xm';
mt=size(Qm,2);
%%%%%%%%
f=[-1;zeros(mt,1)];
A=[[output(i,:)' -Qm];[zeros(K,1) Xm]];
b=[zeros(J,1);input(i,:)'];
%%%%%%%%%%%%%%%%%%%%%
A_eq=[0 ones(1,mt)];
b_eq=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
lb=[0 zeros(1,mt)]';
ub=[];
[para,lambda_minus,exitflag0]=linprog(f,A,b,A_eq,b_eq,lb,ub,[],options);
%Para=[Para; para'];
if exitflag0==1
OTE(i)=-1/lambda_minus;
if OTE(i)>1
count_OTE=count_OTE+1;
OTE(i)=1;
end
else
OTE(i)=0.5;
end
exitflag=[exitflag;exitflag0];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I am calculating technical efficiency
Can anyone kindly tell what is wrong with the above code

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 23 Feb 2015
Edited: MathWorks Support Team on 22 May 2019
To call a function or a script, just write its name with the necessary inputs:
my_function(...)
Or
my_script
This works in the command window, or within another function or script.
You might like to revise the differences between scripts and functions in MATLAB, as these have very different properties! You also have to ensure that the called function can be found by MATLAB, which means it must be on the search path .
  2 Comments
Stephen23
Stephen23 on 23 Feb 2015
Your two functions are in two different Mfiles. If these are functions you should ensure that the function and Mfile names are the same. Then you can simply call one function from the other one (assuming that they are both on the MATLAB search path). If this is one function:
function y = my_square(x)
% square a number
y = x.^2;
end
then we can simply call it in another function like this:
function out = my_fun
a = 3;
out = my_square(a); % <- call the other function
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!