Reputation: 7
I have two different function that send a value to another. My problem is that the random value that is given is same too all parameter.
This is my test_main function
function[] = test_main(x)
for i = 1:x %loop for time
w=rand; %random error generator.
if w<0.5
status=1;
else
status=99;
end
fprintf('\n Time is %d hour after count start . \n', i);
test_sub(status);
end
end
This is my test_sub function
function[a] = test_sub(z)
for variable = 1:4
fprintf('\nPARAMETER %d ', variable)
fprintf('\n Value is %d \n', z);
if (z==1) %if input equal to 0
j=1; %store temporary value to j
a=j;
disp('new value is 1')
elseif (z==99)
j=1;
a=j;
disp('new value is 100')
else
disp('unidentified error')
end
end
end
The code has no error only the looping part is not working. The output from both function is above:
Time is 1 hour after count start .
PARAMETER 1
Value is 99
new value is 100
PARAMETER 2
Value is 99
new value is 100
PARAMETER 3
Value is 99
new value is 100
PARAMETER 4
Value is 99
new value is 100
The integer that was assigned to "value" is the same on all four parameter. I was hoping that each parameter will have different value and it will come out sort of like this:
Time is 1 hour after count start .
PARAMETER 1
Value is 1
new value is 1
PARAMETER 2
Value is 1
new value is 1
PARAMETER 3
Value is 99
new value is 100
PARAMETER 4
Value is 1
new value is 1
Upvotes: 0
Views: 56
Reputation: 35525
Do you realize you are doing the same thing 4 times? That is why you dont get new values, there are not.
You code does:
test_main(1)
I assume 1, or else you are not showing all the outputs. w
if w<0.5
...call test_sub(status)
for variable = 1:4
, or "repeat everything 4 times"disp
4 times for the same status
Why do you have the for loop inside the test_sub
function? The function is supposed to test the result, and should only do that. It should look like:
function[a] = test_sub(z,count)
fprintf('\nPARAMETER %d ', count)
fprintf('\n Value is %d \n', z);
if (z==1) %if input equal to 0
j=1; %store temporary value to j
a=j;
disp('new value is 1')
elseif (z==99)
j=1;
a=j;
disp('new value is 100')
else
disp('unidentified error')
end
end
end
And the one that is responsible of repeating the thing is the one that should! if you want new parameters, change the amount of times it loops, not the amount of times it tests!
test_main(4) % will repeat the test 4 times
also change the call to test_sub()
to test_sub(status,ii);
adn the name i
to ii
(as i
is the imaginary unit in matlab)
Upvotes: 1