Writting strings to Excel

6 vues (au cours des 30 derniers jours)
David
David le 7 Oct 2014
Commenté : Stephen23 le 8 Oct 2014
Hi, I'm trying to write a cell array of strings in Excel, but when I run the code and open the Excel file, it seems that the first string of the cell array is repeated.
Here is the code:
Labels = [PLabels;SLabels]; %cell array of strings (two cell strings joined)
xlswrite('datos de entrada.xlsx',Labels,'Caract.Gnls.','A3');
Thanks for yor help!

Réponse acceptée

Image Analyst
Image Analyst le 8 Oct 2014
Your Labels is not a cell array of strings, despite the fact that your comment claims that. It's a character array and will therefore put one character into each Excel cell like what you're observing. You need to make it a cell array
Labels = {PLabels; SLabels}; % Note braces, not brackets.
Try that and let me know how it worked. Here's a complete demo with 3 cases to see how they work differently:
% This code works just fine.
PLabels = {'aaa', 'bbbb'} % Row vector cell array with 2 columns.
SLabels = {'ccc', 'dddd'} % Row vector cell array with 2 columns.
% Vertically concatenate the two row vectors into a single 2 by 2 cell array.
Labels = [PLabels;SLabels]; % 2 by 2 cell array of strings (two cell array joined)
xlswrite('delete me 1.xlsx',Labels,'Caract.Gnls.','A3');
% Case #2
% This code does not.
PLabels = ['aaa', 'bbbb'] % A single string 'aaabbbb'
SLabels = ['ccc', 'dddd'] % A single string 'cccdddd'
Labels = [PLabels;SLabels]; % Character array of strings
xlswrite('delete me 2.xlsx',Labels,'Caract.Gnls.','A3');
% Case #3
% This code also works just fine, but differently than case #1.
PLabels = ['aaa', 'bbbb'] % A single string 'aaabbbb'
SLabels = ['ccc', 'dddd'] % A single string 'cccdddd'
Labels = {PLabels; SLabels}; % Note braces instead of brackets.
xlswrite('delete me 3.xlsx',Labels,'Caract.Gnls.','A3');
I also highly recommend you re-read this section of the FAQ to get an intuitive feeling with how to deal with cell arrays: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
  2 commentaires
David
David le 8 Oct 2014
Hi!! I also tried to define the cell array with braces, the problem is that if you try to join two cell arrays of strings A and B of size m x 1 and n x 1 into a cell array called C, the result is a cell array of size 2 x 1.
I did an example an took an screenshot
Thank you!
Stephen23
Stephen23 le 8 Oct 2014
What you describe (and show in the screenshot) is the correct, documented behavior. Using {} creates a cell array (in your case containing other cell arrays), whereas [] concatenates the two cell arrays together.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by