Ignore data in string using ~strcmp and ~strncmp

조회 수: 6 (최근 30일)
Tammy Chen
Tammy Chen 2016년 11월 30일
댓글: Tammy Chen 2016년 12월 3일
Hi All, The following is a snippet of my code for data calculation.
for f = pvt[~strcmp(pvt,'False start');~strncmp(pvt,'Wrong key:',9)] %do not calculate false starts or wrong keys
s = str2double(f)
a = mean(s)
b = median(s)
I want Matlab to ignore values such as "False start" or anything that begins with "Wrong key:" and just calculate the mean and median in my data string without them, and output the statistical calculations on excel sheets. With this code above, it will not calculate the mean and medians of any data strings with "False start" or "Wrong key:" in it and will generate "NaN" in for mean/median instead. However, it will calculate the mean & median of data strings with the value "False start" if I change the line to
f = pvt(~strcmp(pvt,'False start'));
but it will still generate "NaN' for mean + median for those data strings with values beginning with "Wrong key:".
How do I code this line so Matlab will calculate the stats for only number values in all data strings and ignore the values "False start" and those beginning with "Wrong key:"?
Thanks,
Tammy

채택된 답변

Walter Roberson
Walter Roberson 2016년 11월 30일
f = pvt(~(strnmp(pvt,'False start') | strncmp(pvt,'Wrong key:', 10)));
  댓글 수: 1
Tammy Chen
Tammy Chen 2016년 12월 3일
Hi, Thanks for this great solution. I'm not good at cell handling at all. I fixed the minor typo in the line and it worked out for my code.
f = pvt(~(strcmp(pvt,'False start') | strncmp(pvt,'Wrong key:', 10)));
I changed strnmp to strcmp that's all.
Regards, Tammy

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by