2

1 つは info{} と呼ばれ、もう 1 つは data{} と呼ばれる 2 つのセル配列があります。テキスト ファイルから情報を読み取り、その行を 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}
4

1 に答える 1

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

于 2010-06-23T04:31:43.933 に答える