concatenate matrixs

1 view (last 30 days)
li xinjun
li xinjun on 7 Dec 2011
I have two matrixs:one is num,the other is str.Like Num1=[1,2.32322232,4;222,0,-Inf],char1=['A1';'A2'].I want concatenate them,and use 'xlswrite' to save them in a excel. My steps:
Num1=[1,2.32322232,4;222,0,-Inf];
char1=['A1';'A2'];
w=str2num(char1);
w=[w;Num1;];
xlswrite('excel',w);
But it shows : ??? Error using ==> vertcat CAT arguments dimensions are not consistent. How can I do?
  1 Comment
li xinjun
li xinjun on 7 Dec 2011
And also,I need Num1=[1,2.32322232,4;222,0,-Inf] show like [1.00,2.32,4.00;222.00,0.00,-Inf] in the excel.I know the length of each elements between char1 and Num1 are not the same ,even each elements of the Num1 isn't the same longth,but I also hope I can achieve.

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 7 Dec 2011
Because you have mixed numerical and string data, you have to make your variable w a cell array if you just want to run xlswrite() once. One way to come up with the cell array with your desired format for numerical data is below.
%%Original data
Num1=[1,2.32322232,4;222,0,-Inf];
char1=['A1';'A2'];
w=textscan(sprintf('%10.2f,',Num1),'%s','delimiter',',');
w=[cellstr(char1), reshape(w{1},size(Num1,1),size(Num1,2))];
xlswrite('ExcelFile.xls',w);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!