0

Here is my code:

friendly_output = num2str(std(counts_channel),'%.4f');
if friendly_output > 0 && friendly_output <= 1000
    variable = 100
elseif friendly_output > 1000 && friendly_output <= 1500
    variable = 500

The variable friendly_output here is a decimal number. However, while I am executing this code, this prompts me the error:

Operands to the || and && operators must be convertible to logical scalar values

I tried to solve the issue by replacing && with &, the program works, but the variable friendly_output failed to catch the correct if statement.

I tried to output the value of friendly_output, the value is correct but the statement it goes into is wrong.

Thank you.

  • 2
    It's not clear why you're using `num2str` in the first place. If your print out your `friendly_output` variable and `size(friendly_output)`, you'll see that it's not a scalar. Now try `help &` and `help &&` in your Command Window. Also, see [this answer](http://stackoverflow.com/a/1379497/2278029). – horchler Apr 26 '15 at 19:21
  • Remove num2str or use str2num on friendly_output. If you want to compares strings, use strcmp. – Arturo Apr 27 '15 at 03:59

1 Answers1

4

If my guess is correct, your friendly_output is of type char

To check that, try this:

class(friendly_output)

If you need to compare it with an integer, you need to convert it back to a number.

To do this add this code after the first line

friendly_output = str2double(friendly_output);
%// changed from `eval` to `str2double` as suggested by @horchler
%// Using `str2double` over `eval` or `str2num` is a best practice.
%// or you could just avoid `num2str` conversion

PS:

The && operator didn't work for you because they work good only on scalar inputs. But as the friendly_output variable is a char array, you got the error.

While & works on array inputs, Each char is first converted to its corresponding ASCII value and then compared with the number. So even though Matlab doesn't post an error, the results won't be favorable to you.

For more information on the difference between & and && Refer Here

Here is an example of what is happening when you don't convert the string back to number:

>> a = '1200.5'

a =

1200.5

>> a > 1000

ans =

 0     0     0     0     0     0

The ASCII values of char 0-9 ranges from 49-57 while ASCII value of char '.' is 46

Although, 1200.5 is greater than 1000, it actually calculate this way

50(char '1') is not greater than 1000.
51(char '2') is not greater than 1000.
49(char '0') is not greater than 1000.
49(char '0') is not greater than 1000.
46(char '.') is not greater than 1000.
54(char '5') is not greater than 1000.

Community
  • 1
  • 1
Santhan Salai
  • 3,888
  • 19
  • 29
  • 1
    There is no "string" class in Matlab. `friendly_output` is a `char` array. And `eval` is the wrong (unsafe and bad practice) way to convert a string representing a floating point value back to double precision. `str2double` or `str2num` should be used for that. You've not explained the cause of the error and why `&&` doesn't work for non-scalar `char` arrays. I also have no idea what the OP is trying to accomplish with `num2str` in the first place though. – horchler Apr 26 '15 at 19:07
  • @horchler, Thanks for pointing out the mistakes instead of just voting down. I have edited the answer. If you still think i lack some information, feel free to make your own answer and i'll upvote it. Also could you explain why using `eval` is a bad practice? – Santhan Salai Apr 27 '15 at 03:59
  • 1
    The main reason not to use `eval` is because there are better, faster, more robust ways of doing the same thing in most cases (this being one). `eval` evaluates an arbitrary string without checking anything about it, which can obviously result in many sorts of problems or vulnerabilities. More here: [1](http://blogs.mathworks.com/videos/2010/03/08/top-10-matlab-code-practices-that-make-me-cry/), [2](http://blogs.mathworks.com/loren/2005/12/28/evading-eval/), and [3](http://www.mathworks.com/matlabcentral/answers/51946-systematic-do-not-use-global-don-t-use-eval). – horchler Apr 27 '15 at 23:51
  • 1
    ... additionally, there are many on this site who will instantly down vote you for misusing `eval` except in one of the very [rare cases where it is necessary or useful](http://stackoverflow.com/questions/1826859/is-there-ever-a-good-reason-to-use-eval). Such cases usually only occur when writing very low-level functions or libraries. For example, Matlab's Symbolic Math toolbox is effectively based on a using a form of `eval` at it's base. – horchler Apr 27 '15 at 23:55
  • Lastly, comparing individual `char` values with logical operators should be avoided if possible – [see here](http://stackoverflow.com/questions/20848814/when-comparing-characters-is-ischarx-x-b-equivalent-to-strcmpx-b). You should use `strcmp` or one of it's relatives. – horchler Apr 27 '15 at 23:59