[英]how to read a number within a string using matlab
I have a file with non standard format and i want to read specific parts of it, that consist of real or integer numbers. 我有一个非标准格式的文件,我想读取文件的特定部分,该部分由实数或整数组成。
str1='N.ELBETI.150.L10';
str2='KARDIA_3.150.L20';
str3='axeloos1.15.75.L20';
str4='florina2.6.3.L2.1';
i just want to store the numbers in the central part and at the end of each string, ie: 我只想将数字存储在每个字符串的中心部分和末尾,即:
150 and 10 from str1, 150 and 20 from str2, 15.75 and 20 from str3, 6.3 and 2.1 from str4 i have tried several ways but i cannot. 我已经尝试了几种方法,但是我不能尝试str1的150和10,str2的150和20,str3的15.75和20,str4的6.3和2.1。 Can you help me ?
你能帮助我吗 ?
An alternative to the already posted, nice answer. 替代已发布的不错的答案。 Apply the following function to each string.
将以下函数应用于每个字符串。
function [num1, num2] = extract_from_string(s)
num1 = str2num(s(find(s == '.',1,'first')+1:find(s == 'L',1,'first')-2));
num2 = str2num(s(find(s == 'L',1,'first')+1:end));
end
Use regexp
like this: 像这样使用
regexp
:
To obtain the first number: 要获得第一个数字:
str1(regexp(str1,'\.\d')+1:regexp(str1,'L\d')-2)
To obtain the last number: 要获取最后一个号码:
str1(regexp(str1,'L\d')+1:end)
These commands also work for the other strings 这些命令也适用于其他字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.