如何将此文本文件放入 MATLAB 中的列表?
我有一个文本文件,想将其导入 MATLAB 并使其成为一个列表:
Person1
name = steven
grade = 11
age= 17
Person2
name = mike
grade = 9
age= 15
Person3
name = taylor
grade = 11
age= 17
有几百个像上面这样的条目。每个都由空行分隔。我想我可以扫描文本并将每个空行之间的信息放入列表中的一个项目中。一旦我有了如下所示的列表,我还希望能够按名字查找每个人。
我想要这样的东西:
x = [Person1 Person2 Person3
name = steven name = mike name = taylor
grade = 11 grade = 9 grade = 11
age = 17 age = 15 age = 17]
这看起来很简单,但到目前为止我一直遇到麻烦。我可能忽略了一些事情。有人有什么想法或建议吗?
I have a text file and would like to import it into MATLAB and make it a list:
Person1
name = steven
grade = 11
age= 17
Person2
name = mike
grade = 9
age= 15
Person3
name = taylor
grade = 11
age= 17
There are a few hundred entries like these above. Each are separated by a blank line. I was thinking I could scan the text and make the information between each blank line into an item in the list. I also would like to be able to look up each person by name once I have a list like the one below.
I want something like:
x = [Person1 Person2 Person3
name = steven name = mike name = taylor
grade = 11 grade = 9 grade = 11
age = 17 age = 15 age = 17]
This seems very straight forward but I have been having trouble with this so far. I may be overlooking something. Anyone have any ideas or advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法可以做到这一点。假设数据文件中的
age
和=
之间应该有空格(与其他字段一样),您可以使用 TEXTSCAN:然后您可以按以下方式处理数据以创建 3-by- 1 个包含字段
'name'
、'grade'
和'age'
的结构数组:上面使用了函数 重塑,CELLFUN,STR2DOUBLE 和 CELL2STRUCT。
There are many ways you could potentially do this. Assuming there is supposed to be a space between the
age
and=
in the data file (like the other fields), you could use TEXTSCAN:Then you could process the data in the following way to create a 3-by-1 structure array with fields
'name'
,'grade'
, and'age'
:The above uses the functions RESHAPE, CELLFUN, STR2DOUBLE, and CELL2STRUCT.
创建一个包含字段“name”、“grade”和“age”的“person”结构,
然后将
fgetl
与regexp
结合使用,几乎与您之前有关基因的问题完全相同。Create a 'person' structure with fields 'name', 'grade', and 'age'
Then use
fgetl
in combination withregexp
almost exactly like your previous question about genes.