Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
xlswrite on non-contiguous cells

Subject: xlswrite on non-contiguous cells

From: Chiara

Date: 4 Mar, 2011 18:47:23

Message: 1 of 5

Hi,
is there a way to write Matlab output on non-contiguous Excel cells?
More specifically, I would like to copy the first element of a vector (my Matlab output) on cell A1 of the excel sheet, the second on cell C1, the third on cell E1, and so on.
Is there any way to do this, other than to use xlswrite for each element of the vector (which slows down things a lot)? I could not find it.
Thanks a lot for helping. chiara

Subject: xlswrite on non-contiguous cells

From: Sadik

Date: 5 Mar, 2011 23:00:22

Message: 2 of 5

Hi Chiara,

I think the best way would be preparing a sparse cell array and write it ONLY ONCE to the excel file. This should be fast.

Very simple example:

data = cell(9,1);

for k = 1:2:9
    data{k} = ['Data ' num2str(k)];
end

xlswrite('TheFile.xls',info);

Best.

Subject: xlswrite on non-contiguous cells

From: Chiara

Date: 6 Mar, 2011 02:34:09

Message: 3 of 5

Thank you Sadik, it's a good idea and it works, but I am having some trouble in exporting the result to Excel. Using your example, everything is fine (even exporting):

data = cell(1,60);
for k = 1:2:60;
    data{k} = ['Data ' num2str(k)];
end;
xlswrite('Output.xlsx', data, 'Sheet1', 'C221:BJ221');

When I adapt this to my case, suppose the original data is stored in a cell array of strings 'data2'. Then :

data = cell(1,60);
i=1;
 for k = 1:2:60;
    data{k} = [data2(i)]; %cannot use k as index for 'data2',
                                  %otherwise I would miss part of data2
     i=i+1;
 end;
xlswrite('Output.xlsx', data, 'Sheet1', 'C221:BJ221');

This creates the cell array (I can see it is correct in Matlab), but returns a blank array in Excel. Any idea why the result is different? In both cases, 'data' is a cell array.
Thanks again, Chiara





"Sadik " <sadik.hava@gmail.com> wrote in message <ikufa6$d81$1@fred.mathworks.com>...
> Hi Chiara,
>
> I think the best way would be preparing a sparse cell array and write it ONLY ONCE to the excel file. This should be fast.
>
> Very simple example:
>
> data = cell(9,1);
>
> for k = 1:2:9
> data{k} = ['Data ' num2str(k)];
> end
>
> xlswrite('TheFile.xls',info);
>
> Best.

Subject: xlswrite on non-contiguous cells

From: Sadik

Date: 6 Mar, 2011 04:23:07

Message: 4 of 5

Probably we need to see your data2. To be representative of your case, could you give the first 10 elements of data2?

Subject: xlswrite on non-contiguous cells

From: Chiara

Date: 6 Mar, 2011 16:07:06

Message: 5 of 5

Hi Sadik, I think I found the problem. In your example, the array contains strings; in mine, the strings have been converted to cells using cellstr. Here's a sample of my 'data2':
'1Apr98_ h18_DETM_D_ No'
'2Apr98_ h18_DETM_D_ Yes'
'3Apr98_ h18_DETM_D_ Yes'
'4Apr98_ h18_DETM_D_ Yes'
'5Apr98_ h18_DETM_D_ No'
By adjusting my previous loop using char(), as shown below, I get the exact result in the Excel spreadsheet.

data = cell(1,60);
i=1;
 for k = 1:2:60;
    data{k} = [char(data2(i))];
     i=i+1;
 end;
xlswrite('Output.xlsx', data, 'Sheet1', 'C221:BJ221');

Thanks so much for helping! Chiara


"Sadik " <sadik.hava@gmail.com> wrote in message <ikv27b$8s9$1@fred.mathworks.com>...
> Probably we need to see your data2. To be representative of your case, could you give the first 10 elements of data2?

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us