필터 지우기
필터 지우기

For loop to write data to excel

조회 수: 6 (최근 30일)
Zhenhui
Zhenhui 2012년 6월 17일
I have a number of matrix (e.g. n=10) dw1,dw2....dw10. they are obtained through equation dw=-dudx-dvdy. I want to use a for loop to write all these matrix to an excel. I did these
for i = 1:n;
dwname = sprintf('dw%d=-dudx%d-dvdy%d;',[i,i,i]);
eval(dwname);
xlswrite('name.xls',dwname,i,'a1:a600');
end
From this loop i got all the dwi. but because dwname is a char, so the data wrote to excel is not the matrix dwi not only a string . I tried to transfer the char name to variable, but didnt make it.
Is there anyone who can help me with this?

채택된 답변

Image Analyst
Image Analyst 2012년 6월 17일
dwname needs to be a cell, not a character array. Put it inside braces like this {dwname}. Or better yet do it this way where I first create the cell array and then write it out just once at the end instead of in every iteration:
% Create a 1 by n cell array called dwname.
n = 5;
for i = 1:n
dwname{i} = {sprintf('dw%d=-dudx%d-dvdy%d;',[i,i,i])}
end
% Write out the cell array all in one shot.
sheetNumber = 1;
range = 'a1';
xlswrite('name.xls', dwname, sheetNumber, range);
  댓글 수: 5
Image Analyst
Image Analyst 2012년 6월 18일
I don't have your data files, or Excel on this computer, so I can't test anything. All I can say is to start with my code and then look at the example in the help for xlswrite to learn how you can construct a cell array that has both numbers and strings. One cell of the cell array can contain either a string (multiple characters), or a single number of an array. The cell cannot contain the entire array if I remember correctly - you have to put each element into its own cell.
Zhenhui
Zhenhui 2012년 6월 18일
Hey, I wrote all the data in cell arrays. Then it works.
Thanks a lot~

댓글을 달려면 로그인하십시오.

추가 답변 (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