简体   繁体   English

在MATLAB中将不同的元胞数组合并为一个

[英]combining different cell arrays into one in MATLAB

I am trying to look for specific characters in an array and print the output into an excel sheet in the same order (ie if there are elements in between without a match it is left blank).我正在尝试查找数组中的特定字符,并以相同的顺序将输出打印到 Excel 工作表中(即,如果中间有没有匹配的元素,则将其留空)。

I used the following code within the loop:我在循环中使用了以下代码:

EDIT:编辑:

[num,txt,~] = xlsread('protein-peptides.xls')
for i=1:size(txt)
 str(i)=txt(i)
 expression='\w*Pyro-glu from E\w*';
 matchStr(i)=regexp(str(i),expression,'match','once');                             
 ArrayOfStrings=vertcat(matchStr{:}); 
end

After the loop:循环后:

xlswrite(filename,ArrayOfStrings,1);

And the output is like below.输出如下所示。

1) The elements without a match are not shown as blank 1) 没有匹配的元素不显示为空白

2) Each word of the match is displayed in a different cell. 2) 匹配的每个单词显示在不同的单元格中。

P   y   r   o   -   g   l   u       f   r   o   m       E
P   y   r   o   -   g   l   u       f   r   o   m       E
P   y   r   o   -   g   l   u       f   r   o   m       E
P   y   r   o   -   g   l   u       f   r   o   m       E

How do I get the blank spaces left out in the matrix and have the entire matching phrase in a single cell in the output?如何在矩阵中省略空格并在输出的单个单元格中包含整个匹配短语?

I tried concatenation of cells but that is printing all the output in a single row but still each character in different cells我尝试连接单元格,但将所有输出打印在一行中,但仍将每个字符打印在不同的单元格中

I know you'd probably prefer to use xlswrite , but I wasn't able to get this to work (at least on OS X).我知道您可能更喜欢使用xlswrite ,但我无法让它工作(至少在 OS X 上)。 It appears that xlswrite automatically strips out empty cells, as you observed.正如您所观察到的, xlswrite似乎会自动去除空单元格。 It also appears to be spreading each cell over multiple columns, which is bizarre and different from the behavior I remember.它似乎也将每个单元格散布在多列上,这很奇怪,与我记忆中的行为不同。 I wonder if there was a recent update (I'm using R2015b).我想知道最近是否有更新(我使用的是 R2015b)。

I was able to get this to work using simple fprintf calls to write a CSV file, which can be opened in Excel.我能够使用简单的fprintf调用来编写一个可以在 Excel 中打开的 CSV 文件。 Note that the termination character ( \\r ) is critical;请注意,终止符 ( \\r ) 很重要; empty cells do not seem to be preserved in Excel if this is replaced by a linefeed.如果用换行符替换空单元格,则 Excel 中似乎不会保留空单元格。 I also refactored your code a bit.我还稍微重构了您的代码。

% Load data from Excel file
[~, txt, ~] = xlsread('protein-peptides.xls');

% Perform analysis
expression='\w*Pyro-glu from E\w*';
for i = 1:length(txt)
    matches(i, 1) = regexp(txt(i), expression, 'match', 'once');
end

% Write data to CSV file
fid = fopen('test.csv', 'w+');
for i = 1:length(matches)
    fprintf(fid, '%s\r', matches{i});
end
fclose(fid);

Input file rows输入文件行

  • Pyro-Flu from E来自 E 的 Pyro-Flu
  • test测试
  • Pyro-Flu from E来自 E 的 Pyro-Flu
  • test测试

Output file rows输出文件行

  • Pyro-glu from E来自 E 的 Pyro-glu

  • Pyro-glu from E来自 E 的 Pyro-glu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM