在 Matlab 中创建嵌套元胞数组?

发布于 2024-09-06 12:52:34 字数 694 浏览 8 评论 0原文

我有两个元胞数组,一个称为 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}

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2024-09-13 12:52:34

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文