Writting strings to Excel

조회 수: 9 (최근 30일)
David
David 2014년 10월 7일
댓글: Stephen23 2014년 10월 8일
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!

채택된 답변

Image Analyst
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
David
David 2014년 10월 8일
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 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 CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by