For loop to write data to excel

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 日

2 投票

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 件のコメント

Zhenhui
Zhenhui 2012 年 6 月 17 日
hi, thanks a lot for the help.
i tried this. but the cell dwname only have the names of 'dw1=-dudx1-dvdy1; ''dw2=-dudx2-dvdy2;'... I didnt see the data. and it is also all blank in excel. i don't know what is the problem.
sorry that i don't have much experience in Matlab. Maybe it is better explain the thing i want to achieve.
Actually I am dealing with PIV image data. I got the velocity data of u, v in many parallel (n) planes. For the u,v in plane i, they are store in sheeti in excel filename.xls. I have to read all the data in all planes and do the surface fitting and then get the dw=-dudx-dvdy.
The code i use is :
% read data from PIV measurement
% input of x, y, u and v
filename='270H-O.xls';
x=xlsread(filename,'1','a1:a639');
y=xlsread(filename,'1','b1:b639');
n=10; % select n parallel planes
rows=639; % data rows
u=zeros(rows,n);
v=zeros(rows,n);
% I put all the u or v in one matrix
for i = 1:n;
sheetname=num2str(i);
u(:,i)=xlsread(filename,sheetname,'e:e');
v(:,i)=xlsread(filename,sheetname,'g:g');
end
%%surface fitting and calculate derivatives du, dv, dw
for i = 1:n;
ufitname = sprintf('fu%d=createSurfaceFit2(x,y,u(:,i));',i);
eval(ufitname);
duname=sprintf('[dudx%d,dudy%d]=differentiate(fu%d,x,y);',[i,i,i]);
eval(duname);
vfitname = sprintf('fv%d=createSurfaceFit2(x,y,v(:,i));',i);
eval(vfitname);
dvname=sprintf('[dvdx%d,dvdy%d]=differentiate(fv%d,x,y);',[i,i,i]);
eval(dvname);
dwname = sprintf('dw%d=-dudx%d-dvdy%d;',[i,i,i]);
eval(dwname)
end
until here I got dwi in all 10 planes and I can see these matix in workspace. But now I got problems to write these dw back to the excel in which plane they came from. That is I want to write dwi to filename. Xls, sheeti, newrange
Sorry for the long text. Hope you can help .
Cheers
Hui
Image Analyst
Image Analyst 2012 年 6 月 17 日
Again, don't use eval(). If you want to see the value of something, just use disp() or list the name on the line without a semicolon and it will print out the value to the command window.
The data didn't display because in your original code you didn't have any data. If you want strings and numbers mixed, just construct your cell array properly, as demonstrated in the help for xlswrite().
I can't test anything here (like your latest code) because I have only Excel "Starter edition" on my home computer and that edition doesn't allow ActiveX control.
Zhenhui
Zhenhui 2012 年 6 月 18 日
Sorry that i still got problems.
I am not trying to print out the values. If i didn't use eval() to express variables out at each step, in the end, it always shows: Undefined function or variable. Or it only show the result of the last step.
I did have data in the original code. But in the cell dwname, it only contain the name (Char) but not the value of each variable. In the loop, for each step, i want to write the results to the excel.
For an easy example, There are matrix f1, f2, f3...fn. What I would like to do is use a loop to write all the matrix into excel. Any solution to this one?
Your help is greatly appreciated.
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 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by