4

I have a matlab script and one of the variables is:

a = 'false' % my string

my goal is to convert such variable into a boolean variable:

a = false % my goal

is there a matlab function that allows me to convert the string into a boolean value?

a = string2boolean('false') % I would like to have something like this

If there are no functions that allow to do that what could be another possible solution?

2
  • 2
    In Matlab it is called logical. Is it always 'true' or 'false'? Why not just strcmpi(a,'true')? Commented Oct 6, 2016 at 7:55
  • 1
    use ~strcmp(a, 'false'). Commented Oct 6, 2016 at 7:55

5 Answers 5

5

You can write one yourself, it's rather simple

function [output]=string2boolean(string)
   if strcmp(string,'false')
     output = false;
   else
     output = true;
   end
end

Additionally you can altogether skip the function and simply

a = strcmp(a,'true'); 
Sign up to request clarification or add additional context in comments.

2 Comments

Or, if OP want's it a bit simpler: string2boolean = @(s) ~strcmpi(s, 'false');
@StewieGriffin That's shorter, but not simpler. Especially to a beginner the code above is more readable.
5

You can misuse strcmpi for this, i.e

 a = strcmpi(a, 'true');

This is a case-insensitive string comparison, so true, True and TRUE are all converted to the logical true. All other strings will be automatically converted to false.

7 Comments

@Federico: And a = ~strcmpi(a, 'false'). Gives a = false if a = 'false'.
@StewieGriffin The line that I wrote also gives a = false if a='false', no reason to write it that superfluous.
So does 'abc' or [1 2 3]. It is possible that there are other options than 'true' and 'false', since OP hasn't answered the question you asked in the comments. PS! I didn't mean OP should have both 'true' and 'false'. But it's the 'false' part that's "guaranteed" to answer OP's question. The 'true' version might be wrong (but probably isn't).
That is weird. If you make a function that converts something to a boolean, it should give either true or false. But Matlab is not typesafe, so you could indeed return anything. That will however be unexpected behavior for future users of your function.
I agree, but there are many strange ways to do things. Suppose OP has: if x == 0; y = [1, 2, 3]; else y = 'false'; end. Also, I agree, it should convert it to true or false, but it's possible that everything that's not false is true, and not the other way around.
|
4

str2num works (tested with Matlab R2018a)

str2num( '1' ) %yields double(1)
str2num( '0' ) %yields double(0)
str2num( 'true' ) %yields logical(1)
str2num( 'false' ) %yields logical(0)

Comments

0

You can define a method/function as follows:

function boolValue = string2boolean(stringValue)

    boolValue = ''
    if strcmpi(string,'false')
        boolValue = false
    end

    if strcmpi(string,'true')
        boolValue = true
    end

7 Comments

Did you even test your function? This cannot work, you are doing an assignment in the if-statement.
@Bernhard Thanks for pointing out, actually I have provided this just by logical thinking and I haven't run in MATLAB, so after assignment operator correction, code will run correctly.
Comparison operator does not work like that in Matlab. Consider removing your answer, it does not make sense.
It would make sense if you changed == to strcmp, but then it will be a plain copy of the other answer. I agree with @Bernhard here.
I am really surprised that a plain wrong answer attracts more upvotes than two correct answers.
|
0

Use ismember:

str = "true";   % 'true' also works
ismember(lower(str), {'true','1'})    % returns 1

This allows alternative true values than supported natively by Matlab's logical:

str = 'yes';
ismember(lower(str), {'true','1','yes','enable'})  % returns 1

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.