Skip to content

Instantly share code, notes, and snippets.

@lordloh
Last active June 16, 2017 21:17
Show Gist options
  • Save lordloh/2ecaf742875dfabcb76d32d47514cce3 to your computer and use it in GitHub Desktop.
Save lordloh/2ecaf742875dfabcb76d32d47514cce3 to your computer and use it in GitHub Desktop.
MATLAB function to convert Boolean values to String.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment