sscanf missing first zero

조회 수: 4 (최근 30일)
Jason 2021년 4월 23일
댓글: Jason 2021년 4월 23일
Hello I need to pull out the number below from the string
val='020550023 MBB'
and then convert it back to a string (so I want to strip any text)
So this is what I want '020550023'
I've used
But it misses the 1st zero.
Any suggestions to also get the first zero if its present?

채택된 답변

Jan 2021년 4월 23일
No, sscanf considers the leading zeros. But numbers are stored in double format in Matlab (and other programming languages as well), which does not consider leading zeros. They are useful for mathematical operations.
If you want to keep the leading zero, you treat the information as a string. Then use corresponding string methods for the manipulation:
val = '020550023 MBB'
s = strtok(s, ' ');
% Or:
index = find(~isstrprop(val, 'digit'), 1) - 1;
s = val(1:index);
  댓글 수: 1
Jason 2021년 4월 23일
perfect, thanks

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

추가 답변 (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