필터 지우기
필터 지우기

Hello, Could anyone please help me with this issue. In this code, I want to save the output results after each increment in excel and I am unable to do it. Thanking you .

조회 수: 1 (최근 30일)
f=0;
t=0;
a11=0;
a22=1;
a33=0;
X=[a11; a22; a33];
a11=0;
a22=1;
a33=0;
V=[]
for f=0:15:180
M=[cosd(f) -sind(f) 0; sind(f) cosd(f) 0; 0 0 1];
K=[1 0 0; 0 cosd(t) -sind(t); 0 sind(t) cosd(t)];
V=M*K*X
writematrix(V,'V.xls')
end

채택된 답변

Voss
Voss 2022년 5월 23일
편집: Voss 2022년 5월 23일
V is a 3-by-1 column vector in each iteration of the for loop, so one way to save them all is to make V a matrix instead, calculate one column of V each time, and save the entire matrix V to file once after the loop.
f=0:15:180;
t=0;
a11=0;
a22=1;
a33=0;
X=[a11; a22; a33];
a11=0;
a22=1;
a33=0;
V=zeros(numel(X),numel(f));
for ii = 1:numel(f)
M=[cosd(f(ii)) -sind(f(ii)) 0; sind(f(ii)) cosd(f(ii)) 0; 0 0 1];
K=[1 0 0; 0 cosd(t) -sind(t); 0 sind(t) cosd(t)];
V(:,ii)=M*K*X;
end
writematrix(V,'V.xls')

추가 답변 (1개)

Jon
Jon 2022년 5월 23일
편집: Jon 2022년 5월 23일
You don't really explain exactly what your problem is (you just say you "can't do it"). I assume your problem you are complaining of is that you keep overwriting the result in your Excel file, so all you end up with in Excel is the final answer. This happens because you call to writematrix is inside of the loop, and since you just keep writing to the same file, you just overwrite the values with each iteration.
I assume you don't want a different file or different sheet for each iteration,but rather to see all of the results in a single sheet of V.xlsx.
To solve this I would suggest you save the data from all of the iterations in a single 3 by number of iterations matrix, where each column holds a result. Then save this one matrix. Like this:
% % % f=0;
t=0;
a11=0;
a22=1;
a33=0;
X=[a11; a22; a33];
% % % a11=0;
% % % a22=1;
% % % a33=0;
f = 0:15:180;
numPoints = numel(f);
V = zeros(3,numPoints); % preallocate array to hold your answer
for k = 1:numPoints
M = [cosd(f(k)) -sind(f(k)) 0; sind(f(k)) cosd(f(k)) 0; 0 0 1];
K = [1 0 0; 0 cosd(t) -sind(t); 0 sind(t) cosd(t)];
V(:,k) = M*K*X; % store results in columns of V
end
% save the final V matrix in an Excel file
writematrix(V,'V.xls')
I have commented out some of your code which doesn't seem to be used for anything
  댓글 수: 3
Jon
Jon 2022년 5월 23일
For future reference, it is better to have a short general description of your problem in the title. Then put the detailed description of your problem in the body of the question.

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by