1

Hi I am a newbie in MATLAB.

I have a variable named predictLabels which has values i.e 1,2,3,4. For each image the value of predictLabels changes. In workspace it shows as predictLabels = '1' etc.

Problem is, when i use if condition to that variable nothing hapens. Partial code given below-

if predictLabels == 1
    imshow(img);
end

The above code is not working. No error showing, even compiler is not entering in if statement. I think there is a function for this kind of condition check.

1 Answer 1

2

The problem is that your variable predictLabels doesn't contain a numeric value. Instead, it appears to be either a character array ('1') or a cell array of characters ({'1'}). I'm guessing it's the latter, which is why it displays as ... = '1' instead of ... = 1. Whichever one it is, you should use strcmp instead of == in your conditional check:

if strcmp(predictLabels, '1')
  imshow(img);
end

If you'd like to check the data type of a variable, you can use the class function:

>> predictLabels = '1';
>> class(predictLabels)

ans =
char

Or you can use whos to check the data for the whole workspace:

>> whos
  Name               Size            Bytes  Class    Attributes

  predictLabels      1x1                 2  char
Sign up to request clarification or add additional context in comments.

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.