views:

63

answers:

1

I am writing a cell array of string into Excel from Matlab. I have a cell array data{} that I am trying to write into Matlab. It should writting three large lengths of strings to excel since the strcmp passes 3 times. Currently it is only writing the last set of strings into excel. data = { {1x25} {1x35} {1x20} } looks like this. Also I would like to be able to write the data into three cells instead of getting copyied into as many cells as there are lines in the element of the cell array. Is this possible to do with Matlab to excel?

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');

Ok that helps I am aware the cell array's are larger than 3 cell range. I am trying to get the Nx1 cell array to fit into one cell in Excel because It needs to correspond to information in an adjacent cell. Is this at all possible to do?

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)

Here is what I am looking to do in excel

+2  A: 

UPDATED ANSWER:

If I understand correctly, it appears that your variable data is a cell array where each cell contains a 1-by-N (or perhaps N-by-1) cell array of strings. If you want to try and fit each of these cell arrays of strings into one cell of a spreadsheet, you are going to need to format each into a single long string first.

Here's an example of how you could format the cell arrays of strings by concatenating them together with a newline between them:

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 

Now that each cell array of strings is just a single long string, each string can be written to a cell of an Excel spreadsheet as follows:

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

And here's what the resulting spreadsheet looks like:

alt text

If you use a space ' ' instead of a newline character, here's what the spreadsheet looks like (after adjusting the row and column widths):

alt text

Functions used in the above code: CELLFUN, STRCAT, CHAR, DEBLANK, XLSWRITE.

gnovice
I have updated my code to fix the problem but am still having problems when I write to excel.
Ben Fossen
I would also suggest using `ExcelCol` for finding the correct column headers. You can download it from the FileExchange here: http://www.mathworks.com/matlabcentral/fileexchange/27182-excel-column-conversionIt makes dealing with Excel so much easier.
JudoWill
I have made a few things more clear about what I am looking for. I will check out ExcelCol
Ben Fossen
@Ben: I've updated my answer to address your newest edit. Hopefully this helps out more.
gnovice