Variable input to MATLAB function resets involuntarily inside while loop. How can I prevent it? - matlab

I have written a function which takes in an integer (int8) as one of the inputs (called iscool). The function runs a while loop and I insert an if-check inside it to break out of the loop. The if-check checks the value of the iscool variable as well and sets the value of imarch to 0 to get out of loop. So basically, the code is something like this.
% Code_snippet
while (imarch == 1)
<some procedures not modifying iscool>
if ((iscool == 0) && (<other condition 1>) && (<other condition 2>))
imarch = 0;
elseif ((iscool == 1) && (<other condition 3>) && (<other condition 4>))
imarch = 0;
end
disp (strcat('Cooling index is: ',num2str(iscool)));
end
The output of the disp command in the first while-loop execution is 0 (which is the input), but it changes to 1 in the subsequent iteration and stays so after that. I have tried removing the if-elseif-end check and the value of iscool stays intact in that case, but I need to have the check in order to be able to get out of the loop. Any sort of help, particularly an insight into why the value might be changing would be great help. Thanks.

Related

How to save and pass a variable in Matlab

I can find / identify all digits in a number using a recursive function.
My issue is trying to sum the number through each recursion.
I had to initialize sum = 0 at the top of my function statement, and when I return through recursion, I'm always resetting sum back to zero. I do not know how to save a variable without first initalizing it.
Code is below;
function output= digit_sum(input)
sum=0
if input < 10
output = input
else
y=rem(input,10);
sum=sum+y
z=floor(input/10);
digit_sum(z)
end
output=sum
end

Is there an quivalent to 'jump' or 'go to' in Matlab?

I have this code:
values_nodelay = no_of_values(2:2:end)
no_of_values_x1 = (find(u~=[u(2:end), u(end)+1]));
no_of_values_x1 = no_of_values_x1(2:2:end)
l = 1;
delay = 2;
values_delay = [];
while l<=length(values_nodelay)
values_delay_temp = values_nodelay(l)-delay;
if delay>values_delay_temp
end
values_delay = [values_delay, values_delay_temp];
l = l+1;
end
values_delay
I need a goto or jump function to the beginning of while, or an equivalent if anyone
knows an easier way, that if delay > values_delay_temp, it won't become part of the final vector values_delay. Instead, I want to skip it and continue again with the while loop.
Rather than using jumps like continue or break, you could just do this:
if delay<=values_delay_temp
values_delay=[values_delay, values_delay_temp];
end
In other words make the "default" behavior of the loop to do nothing, and then only increment your vector when you hit the right condition. It's much clearer and easier to debug.
Also instead of using vector concatenation like you have, I've found it's more efficient to do values_delay(end+1) = values_delay_temp; if you have to grow a vector in a loop.
The continue statement will stop the execution of a loop for the current iteration and continue with the next iteration, e.g.
while foo
# stuff that will execute for all iterations
if bar
continue
end
# stuff that won't execute if bar is false
end
Although, in your specific case, why don't you just issue
values_delay=[values_delay, values_delay_temp];
and increase the loop counter when
delay <= values_delay_temp
is true?

use debugger in matlab

i want to debug following simplest code in matlab and clarify why it executes always if statement
function testfile(x)
if 3<x<6
disp('in the middle of range');
else
disp('out of range');
end
end
i have used following code for debugger
echo testfile on
testfile(-2)
in the middle of range
testfile(6)
in the middle of range
why it does not execute else statement?i have used following code as a test
5<4<8
ans =
1
so does it mean that writing if statement in this style is wrong?a i understood it is same as if 5<4 || 4<8?then it makes clear for me why it executes only if statement and never reaches to else
5<4<8 is evaluated as (5<4)<8. If we resolve the expression in the parentheses first, we have 0<8, which is true. Test with 5<4==0, which evaluates to true.
What you want to do is check whether x is both bigger than 3 and smaller than 6, i.e.
3<x && x<6

Conditional IF/ELSE Statement in Matlab

I was trying to make a simple statement with Matlab as follows:
if TF==1
disp('One'), break
else continue
end
... ... ...
... ... ...
But even if TF is not 1, when I run the command, it doesn't CONTINUE to the rest of the script!! Any help would be appreciated-- Thanks
The continue statement has a very different meaning. Within a loop, like a for or while loop, continue instructs to skip the current round and continue with the next iteration in the loop. So if you remove continue, you will see the behavior that you are expecting. Here is an example:
for k = 1 : 10
if k == 4
% skip the calculation in the case where k is 4
continue
end
area = k * k;
disp(area);
end
When the loop iterates at k == 4, the block calculating the area of the corresponding square is skipped. This particular example is not very practical.
However, imagine you have a list of ten file names, and you want to process each file in this loop "for k = 1 : 10". You will have to try and open each file, but then if you find out the file does not exist, an appropriate way to handle it would be to print a little warning and then continue to the next file.

How to make a loop which display the output for my program automatically

In my program , suppose I fix no. of users N=6 .
Threshold value of SIR SIRo=6; Output generated is SIRmean=10 (suppose).
I want to make a loop so that
if SIRmean < SIRo
disp N=6
else
decrease the counter till SIRmean> SIRo
and display the value of N for which this condition holds true.
end
You can use simple for loop with downcounter and break on condition:
N=6;
for k=N:-1:0
SIRmean = calc_SIR_mean(N);
if SIRmean < SIRo
disp(['N=' k])
break;
end
end
Function calc_SIR_mean should return mean value based on number of users (it is not clear from your question how you receive this value).