Snippets

Bharath Lohray MATLAB function to convert Boolean values to String.

Created by Bharath Lohray
function [ out_cell ] = bool2str( bool_in,varargin)
%BOOL2STR Converts boolean values to string.
%   BOOL2STR(B) returns a cell of 'True' or 'False' corresponding to the
%   boorean values supplied in vector B.
%
%   BOOL2STR(B,{'Noooo!','Yeah!'}) takes custom stings to correspond to the
%   boolean values.
%
%   Example:
%     bool2str(logical([1 0 0 1]),{'Noooo!','Yeah!'})
% 
%     ans =
% 
%       1×4 cell array
% 
%         'Yeah!'    'Noooo!'    'Noooo!'    'Yeah!'
%
%
% Code by Bharath Bhushan Lohray
% https://bharath.lohray.com/
% github: lordloh   |   bitbucket: lordloh
    
    if(nargin==1)
        str_vals={'False','True'};
    elseif(nargin==2)
        str_vals=varargin{1};
    else
        error('Too many inpute arguments.');
    end
    idx=1+uint8(bool_in);
    out_cell=str_vals(idx);
end

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.