How to assign a value to function through another function
이전 댓글 표시
I have one function 'B' with switch case(switch value is 'val'). And there is a other function ('A') which execute function 'B' through the function 'A'
I need to run function 'A' several times through script file. Can anyone help me to perform this task? Example is given below,
function x=B()
switch val
case 1
end
case 2
end
end
function A()
function B()
end
script file,
val =1
if val=1:10
function B()
function A()
val = val+1
end
댓글 수: 2
Stephen23
2018년 7월 5일
well let me simplify this, I do have massive set of functions.
function A, function B and function C.
function A calls function B while running and function B call function C. There is a equation inside function C. let's take this variable as 'x'. I need to run this main function(function A) several times based on variable 'x' values. Furthermore, example code can be seen as below,
function A
% code
function B
end
-
function B
function C
end
-
function C
Time = sqrt(x) + 4;
end
-----------------------------------------------------------------
And Finally I need to execute simulation several times
-----------------------------------------------------------------
x=1
for i=1:10
function A
x=x+1;
end
Jan
2018년 7월 5일
I'm not sure, what your question is. But if you need the value of x in the functions B and c, provide it as inputs, exactly as I have shown in my answer.
답변 (1개)
Jan
2018년 7월 4일
What exactly is the problem?
You do not have to increment the loop counter val inside the for loop and the case blocks are not closed by an end.
function x = B(val)
switch val
case 1
x = rand;
case 2
x = -rand;
otherwise % No SWITCH without OTHERWISE to reduce chances for bugs
x = 0;
end
end
function A(val)
x = B(val)
end
Script file:
if val=1:10
x = B(val)
A(val)
end
So actually the code was nearly oka,y but it did not get clear, what the problem is. If this does not help, please edit the original question and post the real code.
카테고리
도움말 센터 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!