필터 지우기
필터 지우기

What is the way to get only the desired part of the text?

조회 수: 1 (최근 30일)
niniki
niniki 2022년 2월 23일
답변: Stephen23 2022년 2월 23일
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
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.']
text =
'Name: ABC Age: 20 Gender: Male Height: 180cm. Weight: 75.'
splitlines(text)
ans = 5×1 cell array
{'Name: ABC' } {'Age: 20' } {'Gender: Male' } {'Height: 180cm.'} {'Weight: 75.' }
split(text)
ans = 10×1 cell array
{'Name:' } {'ABC' } {'Age:' } {'20' } {'Gender:'} {'Male' } {'Height:'} {'180cm.' } {'Weight:'} {'75.' }

추가 답변 (1개)

Stephen23
Stephen23 2022년 2월 23일
txt = fileread('test.txt');
tkn = regexp(txt,'^(\w+):\s+(\d*\.?\d*)(\S*)','tokens','lineanchors');
tkn = vertcat(tkn{:})
tkn = 5×3 cell array
{'Name' } {0×0 char} {'ABC' } {'Age' } {'20' } {0×0 char} {'Gender'} {0×0 char} {'Male' } {'Height'} {'180' } {'cm.' } {'Weight'} {'75.' } {0×0 char}
vec = str2double(tkn(:,2))
vec = 5×1
NaN 20 NaN 180 75

카테고리

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