Extract variables from a text data and add them to an array to plot a graph

1 回表示 (過去 30 日間)
Alvin John
Alvin John 2021 年 5 月 4 日
回答済み: Julius Muschaweck 2021 年 5 月 4 日
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

回答 (1 件)

Julius Muschaweck
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

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by