필터 지우기
필터 지우기

for loop is not working correctly

조회 수: 1 (최근 30일)
sidra Rafique
sidra Rafique 2020년 6월 7일
답변: Image Analyst 2020년 6월 7일
filename = 'data.xlsx';
sheet = 4;
c=4;
r=16;
A=zeros(r,c);
redt =1.5;
for i=2:length(A)
xlRange = sprintf('A%d:D%d',i,i);
A=xlsread(filename,sheet,xlRange);
m=max(A);
T =A;
idx = A == m;
T(idx) = [];
W_delay=T*redt;
T(i,:)=W_delay;
xlswrite('delay.xlsx',T,'Sheet4');
end
Above is my code and i also have attached an excel data file. problem with this code is ,there is a problem with the for loop. its only printing 1st and last row in excel sheet. but i need all rows.. i am new to this looping structure please help me this. i am really worried about it.
waiting for kind response
thanks
  댓글 수: 1
per isakson
per isakson 2020년 6월 7일
편집: per isakson 2020년 6월 7일
The statement
xlswrite('delay.xlsx',T,'Sheet4');
overwrites the previous result in every iteration. And
T = A;
overwrites T

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

채택된 답변

Image Analyst
Image Analyst 2020년 6월 7일
Try this:
filename = 'data.xlsx';
sheet = 4;
c=4;
r=16;
A=zeros(r,c);
redt =1.5;
T = zeros(r, 3); % Preallocate T
for row=2: r
% Get range: this row from column A to column D.
xlRange = sprintf('A%d:D%d', row, row);
% Read that range from the input workbook.
A = xlsread(filename, sheet, xlRange);
% Get the max value of that range.
m = max(A);
indexOfMax = A == m;
A(indexOfMax) = []; % Remove max value.
W_delay = A * redt; % Multiply by 1.5
% Insert into T matrix.
T(row,:) = W_delay;
end
% Now write the resulting T matrix out to our output workbook.
xlswrite('delay.xlsx',T,'Sheet4');
winopen('delay.xlsx');
It would be even better if you didn't call xlsread() inside the loop, but before the loop and just took A(row, 1:4) in the loop. It would be faster.

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