How can i store the two variable in an excel sheet in every iteration of the loop ?
Show older comments
Suppose We have two variables, y and z. In every iteration of a loop, y and z change their values. We must store the y and z values column-wise for every iteration in an Excel sheet. I attached an example of the code. So We need to store data in a D matrix(50 by 100 (Row by column)). Can you provide a solution to this problem? Your help will be appreciated.
Thank you for your help.
clc
clear
close all
x = rand(1,50).';
n = length(x);
for i = 1:n
y = sin(x);
z = cos(x);
D = [y,z];
end
1 Comment
Dyuman Joshi
on 31 May 2023
Use writematrix
Accepted Answer
More Answers (1)
Diwakar Diwakar
on 31 May 2023
% Initialize the matrix D
D = zeros(50, 100);
% Loop for 50 iterations
for i = 1:50
% Calculate random values for y and z
y = rand();
z = rand();
% Store y and z values in matrix D
D(i, 1) = y;
D(i, 2) = z;
end
% Save the matrix D to a CSV file
filename = 'data.csv';
csvwrite(filename, D);
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!