Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 在Matlab中创建嵌套单元格数组?

我有两个单元格数组,一个叫做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}

回答:

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

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



更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 07:02


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.