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

3 次查看(过去 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

回答(1 个)

Julius Muschaweck
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 CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by