Introduction to Matlab Flag
Flag is a variable that we use as an indication or a signal to inform our program that a specific condition is met; mostly it is a Boolean variable (taking two values: True or False). For example, if we want all the element of an array to be even, then a Flag variable can be set, and it will become False whenever any value in the input array is odd.
Syntax:
Flag = True
Flag = True
Description:
- Flag can be returned as an output of a function or method
- It can also be used as a condition to perform a specific task. The task will be performed continuously if the Flag is True and will stop once the Flag turns to False
Set Flag in Matlab with Different Examples
Let us now understand the code to set Flag in MATLAB using different examples:
Example #1
Let us start with a very basic example of using a Flag variable to control a while loop. For this example, we will follow the following steps:
- Set Flag variable equal to True
- Start the while loop with the condition that Flag should be True
- Use the While loop to display a message
- Set the Flag to False to ensure that loop does not work indefinitely
Syntax:
flag = true
while flag == true
flag=0;
disp(‘Let us learn Flag in MATLAB’);
end
Code:
flag = true
while flag == true
flag=0;
disp('Let us learn Flag in MATLAB');
end
Output:
As we can see in the output, we have obtained our text output displayed once, as expected by us.
Example #2
In this example, we will write code for a command which will be executed continuously after a fixed interval. For this example, we will follow the following steps:
- Call the timer function: Our aim is to create a timer that displays seconds continuously until the Flag variable turns from True to False.
- Set the Flag to True initially.
- Set the Flag to False in the while loop whenever we want the timer to stop.
- Set the Flag to True, to start the timer all over again.
Syntax:
timerFunction = timer ('TimerFcn', 'Flag=false; disp(''1st Task'')',...
'StartDelay', 5);
start (timerFunction)
Flag = true;
startingTime = 1;
while (Flag = = true)
disp (startingTime)
pause (1)
startingTime = startingTime + 1;
end
timerFunction = timer ('TimerFcn', 'Flag = false; disp(''2nd Task'')',...
'StartDelay', 7);
start (timerFunction)
Flag=true;
while (Flag = = true)
disp (startingTime)
pause (1)
startingTime = startingTime + 1;
end
timerFunction = timer('TimerFcn', 'Flag = false; disp(''3rd Task'')',...
'StartDelay', 5);
start (timerFunction)
Flag = true;
while (Flag = = true)
disp (startingTime)
pause (1)
startingTime = startingTime + 1;
end
Code:
timerFunction = timer ('TimerFcn', 'Flag=false; disp(''1st Task'')',...
'StartDelay', 5);
start (timerFunction)
Flag=true;
startingTime=1;
while (Flag==true)
disp (startingTime)
pause (1)
startingTime=startingTime + 1;
end
timerFunction=timer('TimerFcn', 'Flag=false; disp(''2nd Task'')',...
'StartDelay', 7);
start (timerFunction)
Flag=true;
while (Flag==true)
disp (startingTime)
pause (1)
startingTime=startingTime+1;
end
timerFunction=timer('TimerFcn', 'Flag=false; disp(''3rd Task'')',...
'StartDelay', 5);
start (timerFunction)
Flag=true;
while(Flag==true)
disp (startingTime)
pause (1)
startingTime=startingTime+1;
end
Output:
As we can see in the output, we have obtained the timer outputs corresponding to the 3 tasks. The outputs here are controlled using the Flag variable. When implemented in a live editor, every output digit will be displayed at an interval of 1 second.
Example #3
In this example, we will create a while loop to output the alternate numbers between 50 to 90. We will use Flag command to control the while loop and prevent it from running indefinitely. For this example, we will follow the following steps:
- Set Flag to True
- Run the while loop
- Set the Flag to False once we have values till 100
Syntax:
Flag = true;
while Flag == true
x = 50 :2 :90;
Flag = false;
end
Code:
Flag = true;
while Flag == true
x = 50 :2 :90;
disp(x)
Flag = false;
end
Output:
As we can see in the output, we have obtained alternate numbers between 50 to 90 using a while loop which is controlled by the Flag variable.
Recommended Articles
This is a guide to Matlab Flag. Here we also discuss the introduction and set flag in matlab along with different examples and its code implementation. You may also have a look at the following articles to learn more –