EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Flag

Matlab Flag

Priya Pedamkar
Article byPriya Pedamkar

Matlab Flag

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:

ADVERTISEMENT
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

  1. Set Flag variable equal to True
  2. Start the while loop with the condition that Flag should be True
  3. Use the While loop to display a message
  4. Set the Flag to False to ensure that loop does not work indefinitely

Syntax:

flag = true

[Initializing the Flag to True]

while flag == true

[This while loop will be executed only if flag is equal to True]

flag=0;

[Setting the Flag again to ‘0’ (logical False) so that the loop does not work indefinitely]

disp(‘Let us learn Flag in MATLAB’);

[Displaying the test output]

end

[Please note that, the while loop will get terminated here as we have set the Flag to False inside the while loop]

Code:

flag = true
while flag == true
flag=0;
disp('Let us learn Flag in MATLAB');
end

Output:

 Matlab Flag-1.1

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

[Initializing the Timer and setting the FLAG as False. This timer will work only when the Flag is turned to True]

start (timerFunction) 

[Calling the start function to execute the timer]

Flag = true;

[Setting the Flag variable to True]

startingTime = 1;

[Initializing the point from where timer will start]

while (Flag = = true)

[Using the while loop to check over the Flag every time a second is displayed] [Please note that this loop will be executed only if the Flag is True]

disp (startingTime)
pause (1)

[This will set the interval time between every output]

startingTime = startingTime + 1;
end

[1st task will end here]

timerFunction = timer ('TimerFcn', 'Flag = false; disp(''2nd Task'')',...
'StartDelay', 7);

[The Flag is again set to False inside the timer]

start (timerFunction)
Flag=true;

[The flag is turned back to True and the entire process will repeat itself]

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:

Matlab Flag-1.2

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;

[Initializing the Flag and setting it to True]

while Flag == true

[Setting the condition to ensure that the loop runs only as long as the Flag is True]

x = 50 :2 :90;

[Defining the range to be displayed]

Flag = false;

[Setting the Flag to False once all the numbers are displayed]

end

Code:

Flag = true;
while Flag == true
x = 50 :2 :90;
disp(x)
Flag = false;
end

Output:

Matlab Flag-1.3

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 –

  1. Matlab Plot Colors
  2. Heaviside MATLAB
  3. Factorial in Matlab
  4. Fourier Series Matlab
ADVERTISEMENT
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW