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

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.

10 Comments

Is it warning you about "Basic mode" ? Are you running on MS Windows with Excel installed, or are you running on OS-X or Linux, or are you running on MS Windows but without Excel installed ?
MS Windows with Excel installed. No warning about basic mode.
" ,OK| ,OK" is one of the characters i'm trying to write into excel but i'm unable to do so.
The value of file is that an absolute filespec?
@S: Please post the full command, such that we can try to reproduce the problem. Currently you only rephrase as text, what you intented to do, but if there is an error in your code, it will not be revealed then.
S, yes I understand, but I don't think you understand about cells. Please read the FAQ for a good description of them: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
So when you said a = NPV_run{1}, you're taking the contents of that cell, not the cell itself. And as I said in my answer below, you must pass xlswrite a cell, not a string or else you get the problem you are seeing. To get the cell itself, you'd do a = NPV_run(1), with parentheses, not braces. Again, read the FAQ and then run the code in my answer below, and I think you'll understand more and be very happy.
@isakon I am sorry I do not understand what you mean.
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?
@isakon. No. There exists a single file with that name.
That has nothing to do with it. It's because it's a string, not a cell.

Sign in to comment.

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

9 Comments

Thank you for your answer and comment. I tried using a=NPV_run(1) getting a cell, and then used xlswrite to write this cell value a onto a range. Again it didn't get written.
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.
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.
Blanks in a numeric field in Excel are missing numbers, not implicit 0's.
I have the same problem, I am do get the output in excel but data in written in different cells, each character goes to its own cell. I get the following warning
Warning: Could not start Excel server for export.
XLSWRITE will attempt to write file in CSV format.
> In xlswrite (line 174)
In finalCode (line 62)
and it writes data in csv format.
Below is the piece of my code. My output is a mixture of numbers and characters (car's license plate).
license_plate = results.Text
drvLicNum = randi([1000000 10000000],1,1) % Generating number for drivers license number
drvStore = drvLicNum;
string = license_plate + drvLicNum;
fprintf('%s', string)
%Export to Excel option
%load license_plate.mat
license_plate = license_plate(~isspace(license_plate)) %eliminate spaces in betwwen characters
filename = 'C:\Users\Assignment5\Project\data.xlsx';
ca = {license_plate};
%A = [string]; %working
xlswrite(filename,ca);
Evidently you don't have Excel installed, right? Or maybe a free/express version that doesn't allow ActiveX communication.
If it cannot start Excel through ActiveX, either because you are not on MS Windows or because Excel is not installed (or is not installed with the same architecture as MATLAB, e.g., x86 vs x64), then xlswrite() will use Basic mode, which writes csv files and cannot handle a mix of text and numbers.
You can try to convert to cell array of character vectors using C = cellstr(S).
Unfortunately using cellstr() is not useful in situations where Excel is not available.
With R2013b or later, there is a potential workaround:
t = array2table(ca); %ca can include a mix of numeric and char vectors
writetable(t, 'data.xlsx', 'writevariablenames', false)

Sign in to comment.

More Answers (0)

Asked:

S
S
on 24 Apr 2013

Commented:

on 19 Jul 2017

Community Treasure Hunt

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

Start Hunting!