What is the way to get only the desired part of the text?
1 次查看(过去 30 天)
显示 更早的评论
Hello, I'm a student learning matlab on my own.
Please understand even if I don't understand the contents of the question because I lack knowledge.
It's a question.
What is the way to get only the desired part of the text?
For example
text =
Name: ABC
Age: 20
Gender: Male
Height: 180cm.
Weight: 75.
If there's a text file like this,
I'm going to store the information 20 and 180 I need in the variable 'age, height', respectively.
I'm going to use the function "extract After,
age = extractAfter(text,'age : ')
height = extractAfter(text,'height : ')
If I write the code like this, the result I want will not come out.
Can you tell me another way?
采纳的回答
Seth Furman
2022-2-23
Take a look at the split and splitlines functions.
text = ['Name: ABC' newline 'Age: 20' newline 'Gender: Male' newline 'Height: 180cm.' newline 'Weight: 75.']
splitlines(text)
split(text)
0 个评论
更多回答(1 个)
Stephen23
2022-2-23
txt = fileread('test.txt');
tkn = regexp(txt,'^(\w+):\s+(\d*\.?\d*)(\S*)','tokens','lineanchors');
tkn = vertcat(tkn{:})
vec = str2double(tkn(:,2))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!