Remove units from a string

13 次查看(过去 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 个)

Ingrid
Ingrid 2015-11-30
编辑:Ingrid 2015-11-30
just use textscan
rawData = textscan(yourValue,'%f%s');
value = rawData{1};
units = rawData{2};

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by