Export variables of for loop into an excel sheet

3 views (last 30 days)
JG
JG on 10 Nov 2017
Edited: KL on 11 Nov 2017
for i = 0.01:0.01:0.04
A = i*5;
fprintf('%02d \n',A)
end
xlswrite('xlx.xlx',A);
How can I export the values into an excel sheet? Is this the correct formula?

Answers (1)

KL
KL on 10 Nov 2017
You probably want to store your loop iterations in an array.
A = zeros(1,10); %pre-allocate
for k=1:size(A,2) %use A's size
A(1,k) = k*10; %populate (do your computations)
end
xlswrite('filename.xls',A)
  2 Comments
JG
JG on 10 Nov 2017
Got it to work! Thank you!
clc, clear all, close all
A= zeros(1,5);
for i=1:size(A,2)
A(1,i) = i*5;
end
xlswrite('filename.xlx',A)
KL
KL on 11 Nov 2017
Edited: KL on 11 Nov 2017
There's a reason why I used k as the iterator in the for loop. i and j are the default imaginary units and unlike other programming languages, it's better practice to avoid using them as iterators in matlab.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!