Is there an quivalent to 'jump' or 'go to' in Matlab? - 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?

Related

While debugging with an input from the keyboard, I want to able to skip the execution of a few lines of code in MATLAB

Currently I have this excerpt of code I am trying to debug:
if (white_flag == 1)
keyboard;
imshow(rl_img);
N = N+1;
times(times_index,1) = index;
while(white_flag == 1)
imshow(rl_img);
index = index+1;
%%% If statement for when the loop has run out
if (index >= frames)
break
end
%%% Initial Image Pre-Processing
rl_img = ones(mod_height,width);
pre_rl_img = medfilt2(vreader.read(index));
for i = 1:mod_height
for j = 1:width
rl_img(i,j) = pre_rl_img(i,j);
end
end
white_flag = detect_white( rl_img, white_flag );
end
times(times_index,2) = index;
times_index = times_index+1;
else
index = index+ 1;
end
Now, as you can see, the debug keyboard input call keybaord is on the second line. While this allows me to efficiently see each step of the execution of my program, I do not understand how to skip over the part below:
for i = 1:mod_height
for j = 1:width
rl_img(i,j) = pre_rl_img(i,j);
end
end
This is a rather sizable picture(rl_img), so waiting for keyboard input while I scrawl through the code manually wastes a lot of time. Can someone please tell me how to skip the user input execution of these lines of code while I debug the program?
Please do not hesitate to ask me any questions that can clarify this problem. Thank you for all your answers!
The answer is quite straightforward:
you set a breakpoint after the lengthy loop,
when you decide to run automatically all the rest of the loop up to the breakpoint you just set, press [F5] (Continue).
This assumes that you debug your code in the normal MATLAB IDE.
If I understand your problem correctly, you don't want to step through each loop iteration.
You can either follow CST-Link's advice or you can avoid creating additional breakpoints by placing your cursor somewhere after the loop
and clicking
on the editor's debug panel.

How can i use a while loop in matlab?

I have a problem using a while loop. My main objective is to simulate a Fanno flow problem for a case where the length of the tube is longer than required. This means we have to change the Mach number in the middle. My code is the following.
clc
clear all
close all
P1=1;
T1=273;
Cf=0.005;
Dh=0.15;
G=1.4;
M1=3.0;
Lxstar=0;
M2=1;
Lx=0;
My=0;
Lystar=0;
tol=.001;
L = 6.0;
error=0;
fp = ((1-M1^2)/(G*M1^2))+((G+1)/(2*G))*log(((G+1)*M1^2)/(2*(1+(M1^2*(G-1)/(2)))))
Lstar=(fp*Dh)./(4*Cf)
while Lstar<L
Mx=(M1+M2)./2
fp1=((1-Mx^2)/(G*Mx^2))+((G+1)/(2*G))*log(((G+1)*Mx^2)/(2*(1+(Mx^2*(G-1)/(2)))))
Lxstar= (fp1*Dh)./(4*Cf)
Lx= Lstar-Lxstar
My=((sqrt((2+(G-1)*Mx.^2)/(2*G*Mx.^2-(G-1)))));
fp2=((1-My^2)/(G*My^2))+((G+1)/(2*G))*log(((G+1)*My^2)/(2*(1+(My^2*(G-1)/(2)))))
Lystar=(fp2*Dh)./(4*Cf)
error= Lx+Lystar-L
if error<=tol
break
else
Diff=Lx+Lystar
if Diff<L
Mx=Mx+.01
else
Mx=Mx-.01
end
end
end
When I run it goes smoothly - it does everything I want but once it changes the Mx it runs again with the Mx=M1+M2/2 instead of the corrected Mx = Mx+.01 or Mx= Mx-.01
This line: Mx=(M1+M2)./2 is executed on every loop iteration, even after you assign the new value to Mx. You need to move it out of the loop, like this:
Mx=(M1+M2)./2
while Lstar<L
%other code here...
end
Then when you assign the new value to Mx, it won't be immediately overwritten with the old value.

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

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.

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