
Extract variables from a text data and add them to an array to plot a graph
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi all,
how do I extract the values from a a text data( see below), and add them into 2 arrays so that I could plot a graph?
the text data looks like this for eg.
distance=10
time=1
distance=20
time=2
distance=32
time=3
distance=45
time=4
.
.
.
I want to creat an array for distance and another array for time, and later plot a graph. can someone help me please?
Many thanks,
Alvin
댓글 수: 0
답변 (1개)
Julius Muschaweck
2021년 5월 4일
Your example input is attached as SomeNameValueList.txt.
Calling
rv = ReadNameValueList('SomeNameValueList.txt')
plot(rv.distance, rv.time);
gives you a struct rv with, in this example, fields distance and time:
rv =
struct with fields:
distance: [10 20 32 45]
time: [1 2 3 4]
which you can plot to get

The function just reads these lines sequentially and appends the arrays in the same name fields in the rv.
If you had different input like
hopp=42
hopp=44
topp=32
hopp=21
pi=3.1415
# some comment, ignored
; another comment, ignored
you would get
rv =
struct with fields:
hopp: [42 44 21]
topp: 32
pi: 3.1415
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!