简体   繁体   中英

String comparison

I wrote a simple program to learn about sub functions in Matlab. In the process I encountered an error I was hoping to get help with. Here's the function

function result = hyperbolic(string,x)
%HYPERBOLIC calculates the sinh, cosh and tanh of a given value

msg = nargchk(2,2,nargin);
error(msg);

    if string == 'sinh' | string == 'cosh' | string == 'tanh'
        if string == 'sinh'
            result = sinh(x);
        elseif string == 'tanh'
            result = tanh(x);
        elseif string == 'cosh'
            result = cosh(x);
        end
    else
        error('Invalid String');
    end
end

function sinh_output = sinh(x)
sinh_output = 1;
end

function cosh_output = cosh(x)
cosh_output = 2;
end

function tanh_output = tanh(x)
tanh_output = 3;
end

The problem:

When the function checks to see if the entered string is equal to either sinh, tanh, or cosh, it only prints my error message in the else branch if the length of the entered string is equal to the length of sinh,cosh or tanh which is equal to 4. Otherwise, if the entered string has a length not equal to 4 it prints

Error using  == 
Matrix dimensions must agree.

Error in hyperbolic (line 10)
    if string == 'sinh' | string == 'cosh' | string == 'tanh' 

My question:

Why does it not print my error message when the lengths of the two strings do not equal?

PS Ignore the actual values of the result, I was just testing something

To compare strings, use strcmp . For example:

if strcmp(string,'sinh')

The problem with using == with strings is that Matlab interprets that as an equality test between character vectors (a string is a character vector); and that equality relation is only defined if the vectors have equal lengths. If they have different lengths Matlab issues an error; unless one of them is a single character, as pointed out by Dennis in the comments. In the latter case Matlab would compare the single character against each character of the other string.

strcmp accepts strings of equal or different lengths (and of course returns false if the lengths are different, because in that case the strings are different). So it's the way to go to test strings for equality.

According to the matlab documentation you need to use strcmp to compare strings for equality as it will allow to compare strings of different lengths.

To quote ( emphasize is mine):

The relational operators are <, >, <=, >=, ==, and ~=. Relational operators perform element-by-element comparisons between two arrays. They return a logical array of the same size, with elements set to logical 1 (true) where the relation is true, and elements set to logical 0 (false) where it is not.

The operators <, >, <=, and >= use only the real part of their operands for the comparison. The operators == and ~= test real and imaginary parts.

To test if two strings are equivalent, use strcmp, which allows vectors of dissimilar length to be compared.

Also your code can be shortened (you don't need to compare the strings twice):

if strcmp(string, 'sinh')
    result = sinh(x);
elseif strcmp(string, 'tanh')
    result = tanh(x);
elseif strcmp(string, 'cosh')
    result = cosh(x);
else
    error('Invalid String');
end

or as per comment you can use a switch statement:

switch string
    case 'sinh'
        result = sinh(x);
    case 'tanh'
         result = tanh(x);
    case 'cosh'
         result = cosh(x);
    otherwise
         error('Invalid String');
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM