save cell array of strings and doubles to excel file

Hi, a simple question.
How to save a cell array to a *.xls file? my cell array consists of 2 arrays. one is array of strings and the other is double. using the following command does not work and error occurs. (I have attached my features_score file):
xlswrite('features.csv',cell2mat(features_score(:,1)),'A1'); %array of strings
xlswrite('features.csv',cell2mat(features_score(:,2)),'A2'); %array of double
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
Thank you in advance

 Accepted Answer

You can write cells directly to the file. If you're using xlswrite, you should specify a target file with an .xls or .xlsx extension.
Here's a simple way to do it with dummy data. You should be able to use it with your data the same way.
c1 = {'string';'data';'cell'};
c2 = {1;2;3};
xlswrite('features.xls',[c1,c2]);
Assuming c1 and c2 are Mx1 cell arrays, this will write M rows into columns A and B of the first sheet.

8 Comments

Thank you very much for reply.
Unfortunately even this simple code makes error. cell arrays should be matrix. I also changed the code with my settings like the following:
feat = features_score(:,1);
scores=cellfun(@str2double, features_score(:,2));
xlswrite('features.xls',[feat, scores]);
But it still has this error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in PreProcessDataset (line 81)
xlswrite('features.xls',[feat, scores]);
By the way, my strings ar not just one word. they may be a sentence with whitespace and tab delimiters. Do you think if it matters?
The error indicates that "feat" and "scores" do not have the same number of rows. Does that seem normal to you?
but they both have 6678 rows. feat is cell and score is double. (I have already attached the feature_score)
You can't concatenate a cell array and a double array. In the example I gave, c1 and c2 were both cell arrays.
xlswrite('features.xls',feature_score);
feature_score is already in the right format.
Ah, this also makes error for me...
Error using dlmwrite (line 112)
The input cell array cannot be converted to a matrix.
Let me guess. You're using a Mac? Don't use xlswrite unless you're on Windows.
Use fprintf instead to write a delimited text file.
sh as
sh as on 15 Apr 2016
Edited: sh as on 15 Apr 2016
Ah, yes.
Do you mean fprintf for xls file? or change my file to a *.txt file?
A delimited text file such as *.csv. I've provided a solution with fprintf in your other thread:

Sign in to comment.

More Answers (0)

Asked:

on 15 Apr 2016

Edited:

on 15 Apr 2016

Community Treasure Hunt

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

Start Hunting!