MATLAB Answers


I'm trying to write a character in Excel using xlswrite but I am unable to do so.

Asked by S
on 24 Apr 2013

the character is a=' ,OK| ,g' when i use xlswrite(file,a,sheet,range), the function writes a but with spaces. ie , O K | , g . Each alphabet is in a different cell. I want the entire string to come in a single cell. I used a variant xlswrite(file,{a},sheet,range) and the excel file doesn't change.

Please someone help.

Edit: I am executing these through command window: a=NPV_run{1} NPV_run is a 72x1 cell whose first element is ' ,OK| ,g'. Note this element is not the same as 'blank,OK|blank,g'. I compared the two strings and the two blank spaces correspond to a mismatch between the two strings.

I then used xlswrite(filename,a,sheet,range) and the result is blank , O K | blank , g each in a different cell. I also tried to use xlswrite(filename,{a},sheet,range), but in this case nothing is written and the cell remains blank.

Hope this was clear enough.


per isakson
on 25 Apr 2013

Is there a remote possibility that you have more than one excel-file with the same name, i.e. writing to one and checking the another?

on 25 Apr 2013

@isakon. No. There exists a single file with that name.

Image Analyst
on 25 Apr 2013

That has nothing to do with it. It's because it's a string, not a cell.


No products are associated with this question.

1 Answer

Answer by Image Analyst
on 25 Apr 2013
Edited by Image Analyst
on 25 Apr 2013
 Accepted answer

a must be a cell, not a string:

 a = {' ,OK| ,OK'};

For example, this works just fine:

ca = {' ,OK| ,OK'};
ca(2) = {'This is column 2 of ca'}; % Assign another cell.
ca{3} = 'This is column 3 of ca'; % Alternate way of specifying a cell.
sheet = 1;
range = 'B4'; % Wherever you want...
xlswrite('deleteme.xls', ca, sheet, range);


Image Analyst
on 25 Apr 2013

Did you run my demo? It works and it puts the whole string into one single cell, not strung out across multiple Excel cells with one character (letter) per cell. Please give an example of what NPV_run is. Put this before the call to xlswrite():

whos a

tell me what it says.

on 25 Apr 2013

Found the issue. Each element of NPV_run cell array is a string concatenated from 4 different strings. Some of these strings are blank in excel, but for some odd reason they were read as [NaN]. I just converted these NaN values to blanks and it works fine. Thanks for your help Image Analyst.

Walter Roberson
on 26 Apr 2013

Blanks in a numeric field in Excel are missing numbers, not implicit 0's.

Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!