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

조회 수: 6 (최근 30일)
Ajpaezm
Ajpaezm 2018년 1월 4일
댓글: Ajpaezm 2018년 1월 5일
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.

답변 (1개)

Walter Roberson
Walter Roberson 2018년 1월 4일
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
Ajpaezm
Ajpaezm 2018년 1월 5일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by