如何将此文本文件放入 MATLAB 中的列表?

发布于 2024-09-05 09:29:42 字数 565 浏览 3 评论 0原文

我有一个文本文件,想将其导入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韵柒 2024-09-12 09:29:42

有很多方法可以做到这一点。假设数据文件中的 age= 之间应该有空格(与其他字段一样),您可以使用 TEXTSCAN

fid = fopen('people.txt','r');           %# Open the data file
peopleData = textscan(fid,'%s %*s %s');  %# Read 3 columns of strings per row,
                                         %#   ignoring the middle column
fclose(fid);                             %# Close the data file

然后您可以按以下方式处理数据以创建 3-by- 1 个包含字段 'name''grade''age' 的结构数组:

nFields = 3;                                       %# Number of fields/person
fields = peopleData{1}(2:nFields+1);               %# Get the field names
peopleData = reshape(peopleData{2},nFields+1,[]);  %# Reshape the data
peopleData(1,:) = [];                              %# Remove the top row
peopleData(2:nFields,:) = cellfun(@str2double,...  %# Convert strings to numbers
                                  peopleData(2:nFields,:),...
                                  'UniformOutput',false);
x = cell2struct(peopleData,fields,1);              %# Put data in a structure

上面使用了函数 重塑CELLFUNSTR2DOUBLECELL2STRUCT

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:

fid = fopen('people.txt','r');           %# Open the data file
peopleData = textscan(fid,'%s %*s %s');  %# Read 3 columns of strings per row,
                                         %#   ignoring the middle column
fclose(fid);                             %# Close the data file

Then you could process the data in the following way to create a 3-by-1 structure array with fields 'name', 'grade', and 'age':

nFields = 3;                                       %# Number of fields/person
fields = peopleData{1}(2:nFields+1);               %# Get the field names
peopleData = reshape(peopleData{2},nFields+1,[]);  %# Reshape the data
peopleData(1,:) = [];                              %# Remove the top row
peopleData(2:nFields,:) = cellfun(@str2double,...  %# Convert strings to numbers
                                  peopleData(2:nFields,:),...
                                  'UniformOutput',false);
x = cell2struct(peopleData,fields,1);              %# Put data in a structure

The above uses the functions RESHAPE, CELLFUN, STR2DOUBLE, and CELL2STRUCT.

烟花易冷人易散 2024-09-12 09:29:42

创建一个包含字段“name”、“grade”和“age”的“person”结构,

然后将 fgetlregexp 结合使用,几乎与您之前有关基因的问题完全相同。

Create a 'person' structure with fields 'name', 'grade', and 'age'

Then use fgetl in combination with regexp almost exactly like your previous question about genes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文