我基本上有一个大型数据集文件,我想编写一个 MATLAB 脚本来为其创建数据结构。我试图阅读有关在 MATLAB 中使用结构化数组的信息,但我还没有找到如何做到这一点的解决方案。我在 MATLAB 上编写脚本方面并没有太多经验。
已编辑:我的数据集是一个大型项目列表,例如,每个项目都有 10 个不同的特征。例如,假设100,000个房屋列表和给出的特征可以是price
,county
,state
,date
出售时等。此文件采用txt.
、xls.
或任何您喜欢播放的格式。
我想编写一个 MATLAB 脚本,以以下格式创建它的数据结构:
house(i).price
house(i).county
house(i).state
house(i).date
等
任何关于正确方向的建议或教学方法的示例将不胜感激。
这似乎是一个非常合理的问题,而且很容易解决。
文件的格式,确实使这个问题变得容易或困难。 我自己真的不喜欢.xls
这种工作的文件,但我意识到,你得到你得到的。 假设它位于制表符分隔的文本文件中,如下所示:
Price County State Date
100000 Sherlock London 2001-10-01
134000 Holmes Dartmoor 2011-12-30
123456 Watson Boston 2003-04-15
如果我只是将整个内容读入解析字段名称行并使用动态结构命名来制作结构数组。
fid = fopen('data.txt','r');
tline = fgetl(fid);
flds = regexp(tline,'s*','split');
% initialize the first prototype struct
data = struct();
for ii=1:length(flds)
data.(flds{ii}) = [];
end
ii = 1;
% get the first line of data
tline = fgetl(fid);
while ischar(tline)
% parse the data
rowData = regexp(tline,'s*','split');
% we're assuming no missing data, etc
% populate the structure
for jj=1:length(flds)
data(ii).(flds{jj}) = rowData{jj};
end
% since we don't know how many lines we have
% we could figure that out, but we won't now
% we'll just use the size extending feature of
% matlab arrays, even though it's slow, just
% to show how we would do it
tline = fgetl(fid);
ii = ii + 1;
end
fclose(fid)
希望这能让你开始!