MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   使用Matlab将字符串写入excel吗? (https://www.labfans.com/bbs/showthread.php?t=23576)

poster 2019-12-10 20:41

使用Matlab将字符串写入excel吗?
 
我正在从Matlab向Excel中写入字符串的单元格数组。我有一个要写入Matlab的单元格数组数据{}。由于strcmp传递了3次,因此它应该写出三个大长度的字符串才能出类拔萃。目前,它仅将最后一组字符串写入excel。数据= {{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中的一个单元格,因为它需要与相邻单元格中的信息相对应。这有可能吗?

ABCDE 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中做的事情



[B]回答:[/B]

[B]更新的答案:[/B]

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

这是一个示例,说明如何通过将字符串的单元格数组与它们之间的换行符连接在一起来设置其格式:

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 这是生成的电子表格的外观:

[IMG]https://i37.photobucket.com/albums/e77/kpeaton/example_excel.png[/IMG]

如果使用空格' '代替换行符,则电子表格的外观如下(调整行和列的宽度后):

[IMG]https://i37.photobucket.com/albums/e77/kpeaton/example_excel_2.png[/IMG]

上面代码中使用的函数: [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cellfun.html"]CELLFUN[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcat.html"]STRCAT[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/char.html"]CHAR[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/deblank.html"]DEBLANK[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlswrite.html"]XLSWRITE[/URL] 。



[url=https://stackoverflow.com/questions/3105015]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:11

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