views:

43

answers:

1

I have two cell arrays one called info{} and the other is called data{} I am reading information in from a text file and am putting the lines into the info{} cell array. When the program finds a blank line I want to to start over with a new info{} cell array and keep inserting the lines until it find another blank line...

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);

end end

I have included some code it doesn't work but it is what I have gotten so far.I think I think I understand the algorithm I need to use to do this but am having some trouble implementing it. Any ideas?

In the end I want something that looks like

data = { {info1} {info2} {info3}... {infon}
+1  A: 

I think something like this will work, although I can't know for sure without a sample data file:

%# 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

Some of the functions used above: FGETL, DEBLANK, REGEXP, CELLFUN, MAT2CELL.

gnovice
@Ben: I updated the code in my answer a little while ago because I found the same bug. The new code I have above should work now.
gnovice
@gnovice: I just noticed you updated right after I commented. Thanks
Ben Fossen

related questions