View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
logical2cellstr, cellstr2logical

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

5.0 | 1 rating Rate this file 7 Downloads (last 30 days) File Size: 2.15 KB File ID: #24665 Version: 1.3

logical2cellstr, cellstr2logical


Richie Cotton (view profile)


08 Jul 2009 (Updated )

Convert from a logical array to a cell array of 'true'/'false' strings and back again.

| Watch this File

File Information

C = LOGICAL2CELLSTR(TF) takes a logical array TF, and returns a cell array of strings, with the value 'true' wherever TF is true, and 'false' wherever TF is false.

logical2char([true false; false true])
ans =
    'true' 'false'
    'false' 'true'

TF = CELLSTR2LOGICAL(C) takes a cell array of strings and returns a logical array. Where the input value is 'true' (matched case insensitively), then the corresponding return value is true. Likewise, where the input value is 'false' (matched case insensitively), then the corresponding return value is false. An error is thrown if any other strings are contained in c.

TF = CELLSTR2LOGICAL(C, 1) is as above, but the strings are matched case sensitively.

cellstr2logical({'false', 'True'; 'TRUE' 'FAlsE'})
ans =
     0 1
     1 0

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
14 May 2016 Ben Oeveren

c = num2cell(true(thesize)); %keeps the logic
convert back with
logicalarray = cell2mat(c);

08 Jul 2009 Jos (10584)

Jos (10584) (view profile)

No need for for-loops. In logical2cellstr:

c = cell(size(tf));
c(:) = {'false'} ;
c(tf) = {'true') ;

For cellstr2log, take a look at strcmpi or regexp ...

It was pleasure to find good help though with examples!

Comment only
09 Jul 2009 1.2

I've taken Jos's helpful comments on board. Both functions are now verctorised and run much quicker, especially for larger matrices.

10 Jul 2009 1.3

Minor bug fix.

Contact us