I import an excel file into Matlab. Then I expect with the code I write in Matlab, it will export the result in column 3 in the original excel. But nothing appears in column 3 of the file sample2.xlsx. Could you help me?
clear all
clc
A=xlsread('Criteria.xlsx');
B=xlsread('sample2.xlsx');
n=0;
x=0;
for h=1:99
if B(h,2)>50
n=n+1;
x= x+B(h,3);
end
end
x=x/n;
for h=1:99
if B(h,2)>50
B(h,4)=x;
end
end
xlswrite('sample2.xlsx',B)

 채택된 답변

Image Analyst
Image Analyst 2016년 1월 5일

1 개 추천

What is "A" for? You're not even using it.
And column 3 of B has NaNs in it, so x will be a NaN also, which means column 4 is all NaNs. It looks like x is supposed to be the mean of Col 3 of B for rows where B > 50. Is that true? If so, those two for loops can be vectorized:
% Don't read in A - it's not used.
% A=xlsread('Criteria.xlsx');
B=xlsread('sample2.xlsx');
% Determine which rows to average.
rowsToAverage = B(:,2)>50 & ~isnan(B(:,3));
% Get mean in Column 3
meanOfBigNumbers= mean(B(rowsToAverage, 3));
% Set col 4 = means
B(rowsToAverage, 4) = meanOfBigNumbers;
% Overwrite workbook with new B value.
xlswrite('sample2.xlsx',B)
The xlswrite should work.

댓글 수: 5

Dung Le
Dung Le 2016년 1월 5일
편집: Walter Roberson 2016년 1월 5일
I have applied your code with adjustment in order to be appropriate with more conditions (instead of only > 50 as mentioned above), here it is:
clear all
clc
A=xlsread('criteria.xlsx');
B=xlsread('panel.xlsx');
for i=1:89
for j=1:64
for k=1:3
for h=1:2
rowsToAverage = (B(:,5) == A(i,1)) && (B(:,4) == A(j,2)) && (A(k,4) > B(:,2) > A(k,3)) && (A(h,5) < B(:,1) < A(h,6)) && ~isnan(B(:,3));
meanOfBigNumbers= mean(B(rowsToAverage, 3));
B(rowsToAverage, 6) = meanOfBigNumbers;
end
end
end
end
xlswrite('panel.xlsx',B)
Bit it does not work, could you please help me!
Image Analyst
Image Analyst 2016년 1월 5일
You don't need all those loops. That's why I vectorized it. Can you tell me in words what you want to do? Like inspect A and find out if some column in B is more than something in A, and if it is, assign something to something?
Image Analyst
Image Analyst 2016년 1월 5일
Looks like you've found a solution in your parallel thread because you've accepted an answer there. I still claim you don't need all those for loops. But anyway, at least it's working.
Walter Roberson
Walter Roberson 2016년 1월 5일
Dung Le remarks "Thank you your enthusiastic support :)"

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 1월 5일

0 개 추천

Could you confirm that you want to take the mean of column 3 over the places that column 2 is > 50, and that you want to write copies of that mean to column 4 for those same rows? Like
mask = B(1:99,2) > 50;
x = mean(B(mask,3));
h(mask,4) = x;

댓글 수: 3

Dung Le
Dung Le 2016년 1월 5일
Yes, but could you explain why my code doesn't work ?
Image Analyst
Image Analyst 2016년 1월 5일
The code in my answer (different than Walter's code in this Answer) does work - I actually tried it. There is stuff in all 4 columns. Where you originally had NaN's there will be a blank cell.
Dung Le
Dung Le 2016년 1월 5일
Thanks :)

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

카테고리

태그

아직 태그를 입력하지 않았습니다.

질문:

2016년 1월 4일

댓글:

2016년 1월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by