Can you write an array containing both numerical data and a string to excel?

Hi,
I am trying to send an array which contains both numerical data and a string to an excel file.
I begin by reading in the excel file and then append new data to the bottom of my data before sending this back to the excel file. A simplified example is shown:
M=xlsread('Book1.xlsx',1);
N=[0:10];
M=[M;N]; %To add my data N to the bottom of the original M.
xlswrite('Book1.xlsx',M,1,'A2')
This works fine as long as I use only numerical data. However I would like to extend the functionality so that I could also write a variable x to the excel file, where x is a string stored in my workspace.
So my data would look like the following:
N=[0 1 2 3 4 5 6 7 8 9 10 x]
where x is a string.
Is this possible?
Many thanks,
David Graham.

 Accepted Answer

At first you have to create such a vector in Matlab. This is most likely not, what you expect:
N = [0 1 2 3 4 5 6 7 8 9 10 'string']
But you can create a cell array:
N = {0 1 2 3 4 5 6 7 8 9 10 'string'}
This should be accepted by XLSWRITE also, when I read the help text. There is a corresponding example in "doc xlswrite" also.
HELP and DOC are really helpful. It is always worth to ask them, before asking the forum.

1 Comment

Hi,
Thank you for your fast response. I had actually already tried this and ran in to problems which is why I turned to the forum.
When I simplified my code and tried each action step by step I found the problem was not actually when I tried to write to excel. The problem was actually when I tried to append to my data (a mixture of text and numbers) in the desired format.
I have achieved my aim using a combination of reading in text and numbers separately, creating cell arrays and using vertcat.
So although your answer did not solve my problem, it answered the question in the title perfectly.
Thanks again for your quick response.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!