Writting strings to Excel
9 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Image Analyst
2014 年 10 月 8 日
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 件のコメント
Stephen23
2014 年 10 月 8 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!