Skip to Main Content Skip to Search
Product Documentation

Using Logical Arrays in Conditional Statements

Conditional statements are useful when you want to execute a block of code only when a certain condition is met. For example, the sprintf command shown below is valid only if str is a nonempty string:

str = input('Enter input string: ', 's');
if ~isempty(str) && ischar(str)
   sprintf('Input string is ''%s''', str)
end

Now run the code:

Enter input string: Hello
ans =
   Input string is 'Hello'
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS