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 view (last 30 days)
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

Accepted Answer

Voss
Voss on 23 May 2022
Edited: Voss on 23 May 2022
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')

More Answers (1)

Jon
Jon on 23 May 2022
Edited: Jon on 23 May 2022
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 Comments
Jon
Jon on 23 May 2022
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.

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!