Writting strings to Excel
2 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Image Analyst
on 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 Comments
Stephen23
on 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.
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!