2010-06-23 58 views
2

我有兩個單元陣列,一個叫做info {},另一個叫做data {} 我正在從文本文件中讀取信息,並將行放入info {}單元格數組中。當程序找到一個空行,我想用一個新的信息{}單元陣列重新開始,並保持在插入行,直到它找到另一條黑線...在Matlab中創建嵌套單元格數組?

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} 

回答

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 

一些以上使用的功能:FGETL,DEBLANKREGEXP,CELLFUN,MAT2CELL

+0

@Ben:我之前更新了我的答案中的代碼,因爲我發現了同樣的錯誤。我上面的新代碼現在應該可以工作。 – gnovice 2010-06-23 15:57:19

+0

我剛剛注意到你在我評論後立即更新。謝謝 – 2010-06-23 16:15:25