Comparing strings with extra zero

Hello, I am wanting to perform a safety check that 2 strings are the same:
send: 297.043 rec: 297.043
send: 298.030 rec: 298.03
send: 300.833 rec: 300.833
Theres a problem when I try to test for string equivalence i.e. the 298.030 value, not sure how to resolve this.
thanks

답변 (1개)

Saurabh
Saurabh 2024년 11월 13일

0 개 추천

Hi @Jason,
When comparing strings for equivalence, even a small difference in formatting (such as trailing zeros) can cause them to be considered different. In this case, the issue arises with the second pair of strings, where one has a trailing zero and the other does not ("298.030" vs. "298.03").
To perform a safety check that considers such cases, consider converting the strings to numerical values before comparing them. This approach ignores formatting differences like trailing zeros.
Use 'str2double' to convert the string representations of numbers to actual numerical values.
Leverage the following link to know more about function:
I hope this helps.

댓글 수: 3

Thanks, I did actually do that.
So my raw values are actually these:
send =
2.970429350000000e+02
rec =
' 297.043 '
dE =
6.500000000642103e-05
send
send
dE= abs(send-str2double(rec))
if dE<=0.00001
do something
end
Seems I have to do this:
send=str2double(sprintf('%0.3f',send))
Saurabh
Saurabh 2024년 11월 13일
Thanks for the reply,
If 0.00001 is too strict for the application, consider increasing the tolerance to accommodate the precision differences. For example, consider using 0.0001 instead?

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

카테고리

도움말 센터File Exchange에서 Language Support에 대해 자세히 알아보기

제품

릴리스

R2023b

태그

질문:

2024년 11월 13일

댓글:

2024년 11월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by