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 Comments

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
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.

Sign in to comment.

Answers (1)

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.

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!