Remove units from a string

조회 수: 14 (최근 30일)
Vamsi
Vamsi 2015년 11월 30일
편집: Stephen23 2015년 11월 30일
Hello all,
I would like to remove the units (for e.g. seconds) from a string and convert it to double.
For e.g.: 1.00s to 1.00 or 1.25ns to 1.25
Is there any simple way to do that?
Thanks.
  댓글 수: 1
Stephen23
Stephen23 2015년 11월 30일
편집: Stephen23 2015년 11월 30일
You could use my FEX submission sip2num, which converts SI-prefixed strings into numeric, correctly interpreting the SI-prefixes:
>> sip2num('1.00s')
ans =
1
>> sip2num('1.25ns')
ans =
1.2500e-09
It also returns the string parts split by the numeric substrings, and their numbers of significant digits:
>> [num,spl,sgf] = sip2num('1.25ns')
num =
1.2500e-09
spl =
'' 's'
sgf =
3

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

채택된 답변

Thorsten
Thorsten 2015년 11월 30일
s = '1.25ns';
v = sscanf(s, '%f')
  댓글 수: 1
Vamsi
Vamsi 2015년 11월 30일
Thank you very much

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

추가 답변 (1개)

Ingrid
Ingrid 2015년 11월 30일
편집: Ingrid 2015년 11월 30일
just use textscan
rawData = textscan(yourValue,'%f%s');
value = rawData{1};
units = rawData{2};
  댓글 수: 1
Vamsi
Vamsi 2015년 11월 30일
Thanks for the answser

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

카테고리

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