Matt Stein
Matt Stein

Reputation: 91

MATLAB: char(x) yields Conversion to char from logical is not possible

I have some code necessary for my research, and the developer does not have the time to troubleshoot this with me, so I am hoping I can get some help here:

I am thinking this is a MATLAB version issue (possibly) because it seems pretty straight forward. Here's the code that causes the grief:

y = char(x);

The output is:

Error using char

Conversion to char from logical is not possible.

Yep. If I do disp(x) I get: 0

Can anyone tell me if there is a version/syntax/whatever issue here?

Upvotes: 4

Views: 1068

Answers (1)

gnovice
gnovice

Reputation: 125874

You can't really trust the function disp in this case. It will show 0 or 1 for logical values. For example:

>> disp(false)
   0

You should instead test the data type of x using the class function, and I'm sure you'll see it return logical:

>> x = false;
>> class(x)

ans =

logical

If you want to force it to do the conversion anyway, you can convert the logical to a double like so:

y = char(double(x));

However, you will only ever get a null or start of header character (ASCII codes for 0 and 1) as a result.

Upvotes: 3

Related Questions