【问题标题】:Creating nested cell arrays in Matlab?在 Matlab 中创建嵌套单元格数组?
【发布时间】:2010-06-23 03:42:28
【问题描述】:

我有两个元胞数组,一个称为 info{},另一个称为 data{} 我正在从文本文件中读取信息,并将这些行放入 info{} 单元格数组中。当程序找到一个空行时,我想从一个新的 info{} 单元格数组重新开始,并继续插入这些行,直到它找到另一个空行......

global data
global tags
tags{}
data = {};
line = fgets(fid);
counter = 1;
while ischar(line)
   if regexp(line,'/locus_tag=','match','once')
       tags{end+1} = line;

   else

       info{counter} = line;

       if strcmp(newline, line)
           data{end+1} = info;
           info{counter+1}{end+1} = line;
       end
   end
   line = fgets(fid);

结束 结束

我已经包含了一些不起作用的代码,但这是我到目前为止所得到的。我想我理解我需要用来执行此操作的算法,但在实现它时遇到了一些麻烦。有什么想法吗?

最后我想要一些看起来像的东西

data = { {info1} {info2} {info3}... {infon}

【问题讨论】:

    标签: matlab nested cell-array


    【解决方案1】:

    我认为这样的事情会起作用,尽管如果没有示例数据文件我无法确定:

    %# Load all the lines from the file:
    
    allLines = {};            %# An empty cell array to store all lines in the file
    fid = fopen('data.txt');  %# Open the file
    nextLine = fgetl(fid);    %# Get the next line
    while ischar(nextLine)                %# Check for an end-of-file condition
      allLines = [allLines; {nextLine}];  %# Add the line to allLines
      nextLine = fgetl(fid);              %# Get the next line
    end
    fclose(fid);              %# Close the file
    
    %# Remove any trailing whitespace from the lines:
    
    allLines = deblank(allLines);
    
    %# Find tags and remove them:
    
    index = regexp(allLines,'/locus_tag=','once');  %# Index of matches
    index = ~cellfun(@isempty,index);  %# Find where index isn't empty
    tags = allLines(index);            %# Get cells with tags in them
    allLines(index) = [];              %# Remove cells with tags
    
    %# Find empty lines and group non-empty spans into cells:
    
    index = cellfun(@isempty,allLines);  %# Find empty lines
    allLines(index) = [];                %# Remove cells with empty lines
    counts = diff([0; find(index); numel(index)+1]);  %# Get the number of lines
    counts = counts(counts > 1)-1;                    %#   to put in each group 
    data = mat2cell(allLines,counts);    %# Group the non-empty lines
    

    上面用到的一些函数:FGETLDEBLANKREGEXPCELLFUNMAT2CELL

    【讨论】:

    • @Ben:我不久前更新了答案中的代码,因为我发现了同样的错误。我上面的新代码现在应该可以工作了。
    猜你喜欢
    • 1970-01-01
    • 2013-11-16
    • 2017-04-05
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2016-03-29
    相关资源
    最近更新 更多