gpt4 book ai didi

excel - 使用 Matlab 将字符串写入 excel?

转载 作者:太空宇宙 更新时间:2023-11-03 20:22:48 27 4
gpt4 key购买 nike

我正在从 Matlab 将一个字符串元胞数组写入 Excel。我有一个元胞数组数据{},我正试图将其写入 Matlab。由于 strcmp 通过了 3 次,因此它应该写入三个大长度的字符串才能表现出色。目前它只是将最后一组字符串写入 excel。 data = { {1x25} {1x35} {1x20} } 看起来像这样。此外,我希望能够将数据写入三个单元格,而不是将其复制到与单元格数组元素中的行一样多的单元格中。这可能与 Matlab 相关吗?

done = {}

for i = 1:3
q = strcmp(x_gene_ID{i},locus_tags{i});
if q ==1
done{end+1} = data{i};
disp(done);

end


end


w = xlswrite('data.xlsx',done','E2:E400');

好的,这有助于我知道元胞数组大于 3 个元胞范围。我试图让 Nx1 单元格数组适合 Excel 中的一个单元格,因为它需要对应于相邻单元格中的信息。这完全有可能吗?

A     B      C        D                   E  
w Rv0146 na Rv0039c (i want the cell array1 to go here)
s Rv0156 na Rv0029c (i want the cell array2 to go here)
s Rv0156 na Rv0029c (i want the cell array2 to go here)

这是我想在 excel 中做的事情

最佳答案

更新的答案:

如果我理解正确,您的变量 data 似乎是一个元胞数组,其中每个元胞包含一个 1×N(或者可能是 N×1)字符串元胞数组。如果您想尝试将这些字符串元胞数组中的每一个放入电子表格的一个单元格中,您将需要先将每个字符串格式化为一个长字符串。

这是一个示例,说明如何通过将它们连接在一起并在它们之间换行来格式化字符串元胞数组:

data = {{'hello' 'hi' 'hey'} ...              %# Sample cell array of 1-by-N
{'world' 'earth' 'everyone'} ... %# cell arrays of strings
{'blah' 'blah'}};
data = cellfun(@(x) {strcat(x,{char(10)})},data); %# Add newline characters
%# to the string ends
data = cellfun(@(x) {deblank([x{:}])},data); %# Concatenate the inner cells and
%# remove the trailing newlines

现在每个字符串元胞数组只是一个长字符串,每个字符串都可以写入 Excel 电子表格的一个单元格,如下所示:

xlswrite('data.xls',data(:),'Sheet1','E2');  %# Write the data to cells E2 to E4

生成的电子表格如下所示:

alt text

如果您使用空格 ' ' 而不是换行符,电子表格如下所示(调整行宽和列宽后):

alt text

上面代码中使用的函数:CELLFUN , STRCAT , CHAR , DEBLANK , XLSWRITE .

关于excel - 使用 Matlab 将字符串写入 excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3105015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com