MATLAB Answers

0

Can't use strcmp with two strings or char values that are apparently equal.

Asked by Ajpaezm on 4 Jan 2018
Latest activity Commented on by Ajpaezm on 5 Jan 2018
I have a group of business days in format 'mm/dd/yyyy', from this, I created an array with a char format ( data_char) and a string format ( data_string). I have another much smaller group of dates also presented in two arrays with char ( date_vector_char) and string ( date_vector_str) formats. I want to know how many dates from the second group are present in the first group, and get their indices with a loop.
Without writing the loop, what I intend to do is this:
idx_1=find(strcmp(data_str,date_vector_str(1))==1);
idx_2=find(strcmp(data_char,date_vector_char(1))==1);
But the outcome is an empty array. When I hardcode a date value into the strcmp function, I get an index, but if I pick one value of my array, I get nothing.
Why is this? Can somebody help me? Thanks for your time.

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 4 Jan 2018

I recommend that you switch to ismember instead of strcmp. Note the second output of ismember is the index of the element.
Meantime you might be having problems with nonprinting characters. Take the variable holding the character vector and add 0 to it in order to convert it to numeric and examine the numbers returned. Any number less than 32 is nonprinting and 32 is space. Some values from 127 to 159 are nonprinting. Values above that are a mix of printing and not. If you see any value above 127 then you probably were not expecting that.

  1 Comment

I tried another approach, since I couldn't implement what you told me here.
I used datenum and transformed all dates to numeric format and then used find. It worked nicely.

Sign in to comment.