![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/606995/image.png)
Extract variables from a text data and add them to an array to plot a graph
1 次查看(过去 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/606995/image.png)
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 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!