Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动态生成结构的变通方法-Matlab/Octave问题_Matlab_Indexing_Data Structures_Octave_Cell Array - Fatal编程技术网

动态生成结构的变通方法-Matlab/Octave问题

动态生成结构的变通方法-Matlab/Octave问题,matlab,indexing,data-structures,octave,cell-array,Matlab,Indexing,Data Structures,Octave,Cell Array,我有很多.csv格式的数据集,这些数据集是按照文件名标准组织的,这样我就可以第二次使用正则表达式了。然而,我遇到了一个小问题。我的数据文件的标题是“2012001_C335_2000MHZ_P_1111.CSV”。有四年的兴趣,两个频率,和四个不同的C335风格的标签来描述位置。我要对每个文件进行大量的数据处理,所以我想把它们都读入一个巨大的结构中,然后对其中的不同部分进行处理。我在写: for ix_id = 1:length(ids) for ix_years = 1:2:length(i

我有很多.csv格式的数据集,这些数据集是按照文件名标准组织的,这样我就可以第二次使用正则表达式了。然而,我遇到了一个小问题。我的数据文件的标题是“2012001_C335_2000MHZ_P_1111.CSV”。有四年的兴趣,两个频率,和四个不同的C335风格的标签来描述位置。我要对每个文件进行大量的数据处理,所以我想把它们都读入一个巨大的结构中,然后对其中的不同部分进行处理。我在写:

for ix_id = 1:length(ids)
 for ix_years = 1:2:length(ids_years{ix_id})
  for ix_frq = 1:length(frqs)
   st = [ids_years{ix_id}{ix_year} '_' ids{ix_id} '_' ids_frqs{ix_id}{ix_frq}'_P_1111.CSV'];
   data.(ids_frqs{ix_id}{ix_frq}).(ids{ix_id}).(['Y' ids_years{ix_id}{ix_year}]) =...
        dlmread(st);
  end
 end
end
所有
id
变量都是1x4单元格数组,其中每个单元格都包含字符串

这会产生以下错误: “错误:无法进一步索引cs列表” 和 “错误:对cs列表的分配在多个分配之外无效”

我在网上搜索了这些错误,找到了一些日期从2010年到2012年的帖子,比如和,作者认为这是Octave本身的问题。我可以通过删除ix_frq上最内层的for循环并用替换以“st”开头的行和以“data”开头的行来定义两个单独的结构

因此,当我尝试创建一个更嵌套的结构时,似乎出现了问题。我想知道这是八度音阶所独有的,还是在Matlab中是一样的,还有一个比定义两个单独的结构更灵活的解决方法,因为我希望这是尽可能可移植的。如果您对错误消息的含义有任何见解,我也对此感兴趣。谢谢

编辑:这是完整的脚本-现在生成几个虚拟的.csv文件。在倍频程v上运行。3.8

clear all
%this program tests the creation of various structures.  The end goal is to have a structure of the format frequency.beamname.year(1) = matrix of the appropriate file
A = rand(3,2);
csvwrite('2009103_C115_1500MHZ.CSV',A)
csvwrite('2009103_C115_2000MHZ.CSV',A)
csvwrite('2010087_C115_1500MHZ.CSV',A)
csvwrite('2010087_C115_2000MHZ.CSV',A)
csvwrite('2009103_C335_1500MHZ.CSV',A)
csvwrite('2009103_C335_2000MHZ.CSV',A)
csvwrite('2010087_C335_1500MHZ.CSV',A)
csvwrite('2010087_C335_2000MHZ.CSV',A)

data = dir('*.CSV');  %imports all of the files of a directory
files = {data.name};  %cell array of filenames
nfiles = numel(files);

%find all the years
years = unique(cellfun(@(x)x{1},regexp(files,'\d{7}','match'),'UniformOutput',false));  
%find all the beam names
ids = unique(cellfun(@(x)x{1},regexp(files,'([C-I]\d{3})|([C-I]\d{1}[C-I]\d{2})','match'),'UniformOutput',false));
%find all the frequencies
frqs = unique(cellfun(@(x)x{1},regexp(files,'\d{4}MHZ','match'),'UniformOutput',false));

%now, vectorize to cover all the beams
for id_ix = 1:length(ids)
  expression_yrs = ['(\d{7})(?=_' ids{id_ix} ')'];
  listl_yrs = regexp(files,expression_yrs,'match');
  ids_years{id_ix} = unique(cellfun(@(x)x{1},listl_yrs(cellfun(@(x)~isempty(x),listl_yrs)),'UniformOutput',false));  %returns the years for data collected with both the 1500 and 2000 MHZ antennas along each of thebeams
  expression_frqs = ['(?<=' ids{id_ix} '_)(\d{4}MHZ)']; 
  listfrq = regexp(files,expression_frqs,'match'); %finds every frequency that was collected for C115, C335
  ids_frqs{id_ix} = unique(cellfun(@(x)x{1},listfrq(cellfun(@(x)~isempty(x),listfrq)),'UniformOutput',false));
end

%% finally, dynamically generate a structure data.Beam.Year.Frequency
%this works
for ix_id = 1:length(ids)
  for ix_year = 1:length(ids_years{ix_id})
    data1500.(ids{ix_id}).(['Y' ids_years{ix_id}{ix_year}])=dlmread([ids_years{ix_id}{ix_year} '_' ids{ix_id} '_' ids_frqs{1}{1} '.CSV']);
    data2000.(ids{ix_id}).(['Y' ids_years{ix_id}{ix_year}])=dlmread([ids_years{ix_id}{ix_year} '_' ids{ix_id} '_' ids_frqs{1}{2} '.CSV']);
  end
end

%this doesn't work
for ix_id=1:length(ids)
  for ix_year=1:length(ids_years{ix_id})
    for ix_frq = 1:numel(frqs)
        data.(['F' ids_frqs{ix_id}{ix_frq}]).(ids{ix_id}).(['Y' ids_years{ix_id}{ix_year}])=dlmread([ids_years{ix_id}{ix_year} '_' ids{ix_id} '_' ids_frqs{ix_id}{ix_frq} '.CSV']);
    end 
  end
end
全部清除
%该程序测试各种结构的创建。最终目标是具有相应文件的格式frequency.beamname.year(1)=矩阵的结构
A=兰特(3,2);
csvwrite('2009103_C115_1500MHZ.CSV',A)
csvwrite('2009103_C115_2000MHZ.CSV',A)
csvwrite('2010087_C115_1500MHZ.CSV',A)
csvwrite('2010087_C115_2000MHZ.CSV',A)
csvwrite('2009103_C335_1500MHZ.CSV',A)
csvwrite('2009103_C335_2000MHZ.CSV',A)
csvwrite('2010087_C335_1500MHZ.CSV',A)
csvwrite('2010087_C335_2000MHZ.CSV',A)
数据=目录('*.CSV');%导入目录中的所有文件
文件={data.name};%文件名的单元格数组
nfiles=numel(文件);
%寻找所有的岁月
years=unique(cellfun(@(x)x{1},regexp(文件,'\d{7}','match'),'UniformOutput',false));
%找到所有梁的名称
ids=unique(cellfun(@(x)x{1},regexp(files),([C-I]\d{3})|([C-I]\d{1}[C-I]\d{2}),'match'),'UniformOutput',false));
%找到所有的频率
frqs=unique(cellfun(@(x)x{1},regexp(文件,'\d{4}MHZ','match'),'UniformOutput',false));
%现在,矢量化以覆盖所有梁
对于id_ix=1:长度(id)
表达式_yrs=['(\d{7})(?='ids{id_ix}');
listl_yrs=regexp(文件,表达式,'match');
ids_years{id_ix}=unique(cellfun(@(x)x{1}),listl_yrs(cellfun(@(x)~isempty(x),listl_yrs)),'UniformOutput',false));%返回沿每个波束使用1500和2000 MHZ天线收集的数据的年份

表达式_frqs=['(?问题在于,当您到达导致问题的for循环时,数据已经存在并且是一个结构数组

octave> data
data =

  8x1 struct array containing the fields:

    name
    date
    bytes
    isdir
    datenum
    statinfo
当您从结构数组中选择字段时,您将得到一个cs列表(逗号分隔列表),除非您还为结构数组中的哪个结构编制索引。请参阅:

octave> data.name
ans = 2009103_C115_1500MHZ.CSV
ans = 2009103_C115_2000MHZ.CSV
ans = 2009103_C335_1500MHZ.CSV
ans = 2009103_C335_2000MHZ.CSV
ans = 2010087_C115_1500MHZ.CSV
ans = 2010087_C115_2000MHZ.CSV
ans = 2010087_C335_1500MHZ.CSV
ans = 2010087_C335_2000MHZ.CSV
octave> data(1).name
ans = 2009103_C115_1500MHZ.CSV
所以当你这样做的时候:

data.(...) = dlmread (...);
您没有在左侧得到您所期望的,您将得到一个cs列表。但我猜这是偶然的,因为
数据目前只有文件名,所以只需创建一个新的空结构:

data = struct (); # this will clear your previous data
for ix_id=1:length(ids)
  for ix_year=1:length(ids_years{ix_id})
    for ix_frq = 1:numel(frqs)
        data.(['F' ids_frqs{ix_id}{ix_frq}]).(ids{ix_id}).(['Y' ids_years{ix_id}{ix_year}])=dlmread([ids_years{ix_id}{ix_year} '_' ids{ix_id} '_' ids_frqs{ix_id}{ix_frq} '.CSV']);
    end 
  end
end

我还建议您更好地考虑您当前的解决方案。在我看来,这段代码过于复杂。

您得到的错误是,在索引过程中,您必须索引一个无法索引的。您能否为您的代码提供示例输入,以便我们复制问题并提供更多帮助?目前有点复杂。您好@carandraug-感谢您查看。我已经发布了上面的完整代码,尽管我不确定这是否能回答您的问题。我认为,如果是cs列表问题,那么问题一定是在ix_frq的频率循环
,但我就是看不到。也许您误解了我。您能举一个定义a的例子吗在进入循环之前,进行小测试
ids
ids\u years
,这样我们就可以在我们自己的计算机上复制错误?啊,我的错误-
ids={'C115';'c355';'D115';'G5F55'}
ids\u years={'2010087';'2012001';'2013002';'2014003'
,以及
ids\frqs='1500MHZ'>
查看循环中使用的变量,并给出复制错误所需的变量。这还不够。你能不能放一段代码,当进入一个新的倍频程会话时,它会显示你的问题?这看起来很好,很有意义。我很惊讶我忽略了这一点-现在看起来很明显,但正如你所说,代码是正确的太复杂了。这是一组不断变化的数据的结果,这些数据可能包括不同的频率和不同的位置,因此从文件名构建结构似乎更容易。我非常感谢您的帮助!
data = struct (); # this will clear your previous data
for ix_id=1:length(ids)
  for ix_year=1:length(ids_years{ix_id})
    for ix_frq = 1:numel(frqs)
        data.(['F' ids_frqs{ix_id}{ix_frq}]).(ids{ix_id}).(['Y' ids_years{ix_id}{ix_year}])=dlmread([ids_years{ix_id}{ix_year} '_' ids{ix_id} '_' ids_frqs{ix_id}{ix_frq} '.CSV']);
    end 
  end
end