필터 지우기
필터 지우기

XLSwrite function

조회 수: 1 (최근 30일)
Maeve  Ryan
Maeve Ryan 2011년 11월 5일
Hello, I am trying to export the results of my code into an excel file using the xlswrite function. However, instead of having 17520 different values, it just plots the first value into all excel cells. Can anyone help?
Here is my code;
function [d] = DOC_export
b = xlsread( 'Cumulative_flow.xlsx',1,'B2:B17521');
c = xlsread ('Cumulative_flow.xlsx',1, 'C2:C17521');
for i = 1:17518;
d(1) = b(1)*c(1);
d(i+1) = (b(i+1)-b(i)) * c(i+1);
d(17520) = (b(17520)-b(17519))*c(17520);
end
e = d;
xlswrite ('Cumulative_flow.xlsx',e,1,'D2:D17521')

채택된 답변

John Kitchin
John Kitchin 2011년 11월 5일
You need to transpose e:
xlswrite ('Cumulative_flow.xlsx',e',1,'D2:D17521')
alternatively, create your d vector in the same shape as b and c before you enter the loop:
d = zeros(size(c));
so it is created as a column. It looks like you could take the first and third lines out of your loop, since they don't depend on the loop counter. No point in calculating them 17518 times each!
  댓글 수: 2
John Kitchin
John Kitchin 2011년 11월 5일
hehe... someone else posted the same thing while I was writing my answer!
Maeve  Ryan
Maeve Ryan 2011년 11월 11일
Thanks that worked!

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

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 5일
The problem is:
Your resulting e is 1x17520, a row vector. Your specified position is 'D2:D17521', a column vector.
This could be an improvement of xlswrite. But anyway, that is the cause of the problem. If you write the transpose of e, the result will be correct. xlswrite ('Cumulative_flow.xlsx',e',1,'D2:D17521')
Or you can directly pre-allocate d as a column vector d=zeros(17520,1)
Two other things:
  1. You should pre-allocate d as above, because d is a pretty large vector.
  2. You need to move the first and third line inside the for-loop out of the loop.
  댓글 수: 1
Maeve  Ryan
Maeve Ryan 2011년 11월 11일
Okay thanks that works perfectly. What do you mean by pre-allocating d?
Thanks

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

카테고리

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