how can call a function

조회 수: 1 (최근 30일)
brahmi ibtissem
brahmi ibtissem 2019년 8월 18일
댓글: Walter Roberson 2019년 8월 19일
Hello everyone,
how can i call this function in an other function?
i need to plot a, b, c and d:
function [o, a, b, c, d] = ObjectiveFunction(X )
a=sum(throughputS, 'all');
b=sum(throughputT, 'all');
c=sum(throughputCN, 'all');
d=sum(throughputCR, 'all');
Thanks alot
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 8월 18일
Where are throughputS, throughputT, throughputCN, and throughputCR coming from, and why is the input X not being used? Where is the output o coming from?
Your outputs a, b, c, and d are all going to be scalars, so it is not obvious what you would be plotting?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2019년 8월 18일
Example:
N = 100;
o = zeros(N,1); a = zeros(N,1); b = zeros(N,1); c = zeros(N,1); d = zeros(N,1);
for idx = 1 : N
X = rand(15,15);
[o(idx), a(idx), b(idx), c(idx), d(idx)] = ObjectiveFunction(X);
end
plot([a,b,c,d])
legend({'a', 'b', 'c', 'd'});

Luna
Luna 2019년 8월 19일
You should define the variables you are going to use as inputs of function. The inputs are written in the paranthesis.
All outputs must be assigned also.
function [o, a, b, c, d] = ObjectiveFunction(throughputS,throughputT,throughputCN,throughputCR)
a=sum(throughputS, 'all');
b=sum(throughputT, 'all');
c=sum(throughputCN, 'all');
d=sum(throughputCR, 'all');
o = 'Luna';
end
Then you call this function as follows in another function:
function hPlot = anotherFunctionToPlotSomeData(w,x,y,z) % you should pass variable reference here.
[o, a, b, c, d] = ObjectiveFunction(w,x,y,z) % where your w,x,y,z becomes throughputS,throughputT,throughputCN,throughputCR respectively inside this function.
% Now you obtained a,b,c,d (I don't know what to do with "o" variable but you get the idea)
figure;
hPlot = plot([a,b,c,d]); % this function also returns the plot handle (line class)
end
Also check below link to learn more about how to create functions:
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 8월 19일
The a, b, c, d returned are all going to be scalars, so plot([a,b,c,d]) would be a request to plot a line with four points. Although that is well defined, I suspect that is not what the poster had in mind.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by