Writting strings to Excel
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
David
am 7 Okt. 2014
Kommentiert: Stephen23
am 8 Okt. 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!
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 8 Okt. 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 Kommentare
Stephen23
am 8 Okt. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!