how to use writetable for one sheet?
12 views (last 30 days)
Show older comments
i tried many things but still cant use writetable for one sheet...whatever i do it gives me last data only....i need all ten data in one single sheet and i need five first row to be empty i excel...
clear all
clc
inputData = [0.001 0.1;
0.003 0.1;
0.006 0.1;
0.1 0.09;
0.3 0.08;
0.5 0.07;
5 0.01;
7 0.005;
8 0.001;
10 0.0001];
numSets = 10; % Number of sets to generate
% Initialize an empty matrix to store the generated data
outputData = zeros(size(inputData, 1), 2, numSets);
% Path to the Excel file
excelFilePath = 'D:\generate_sigma.xlsx';
% Generate and write data to separate sheets
for i = 1:numSets
% Generate similar data with increasing amplitude
scalingFactor = i * 0.1;
generatedData = scalingFactor * inputData(:, 1);
sigma = scalingFactor * inputData(:, 2);
% Store the generated data in the output matrix
outputData(:,:,i) = [generatedData, sigma];
% Create a table for the generated data
Tm = table(outputData(:,1,i), outputData(:,2,i), 'VariableNames', {'a', 'b'});
% Write the table to a separate sheet in the Excel file
writetable(Tm, excelFilePath, 'Sheet', i);
% Plot the data
loglog(outputData(:,1,i), outputData(:,2,i), 'DisplayName', sprintf('Generated Data Set %d', i));
hold on; % Keep the plot hold on for the next data set
end
% Adjust plot settings
grid on;
legend('Location', 'best');
hold off; % Release the plot hold
% Display the generated data for each set
for i = 1:numSets
disp(['Generated Data Set ', num2str(i)]);
disp(outputData(:,:,i));
end
0 Comments
Accepted Answer
Chunru
on 27 Nov 2023
inputData = [0.001 0.1;
0.003 0.1;
0.006 0.1;
0.1 0.09;
0.3 0.08;
0.5 0.07;
5 0.01;
7 0.005;
8 0.001;
10 0.0001];
numSets = 10; % Number of sets to generate
% Initialize an empty matrix to store the generated data
outputData = zeros(size(inputData, 1), 2, numSets);
% Path to the Excel file
excelFilePath = 'generate_sigma.xlsx';
% Generate and write data to separate sheets
for i = 1:numSets
% Generate similar data with increasing amplitude
scalingFactor = i * 0.1;
generatedData = scalingFactor * inputData(:, 1);
sigma = scalingFactor * inputData(:, 2);
% Store the generated data in the output matrix
outputData(:,:,i) = [generatedData, sigma];
% Create a table for the generated data
Tm = table(outputData(:,1,i), outputData(:,2,i), 'VariableNames', {'a', 'b'});
% Write the table to a separate sheet in the Excel file
writetable(Tm, excelFilePath, 'Sheet', i);
% Plot the data
loglog(outputData(:,1,i), outputData(:,2,i), 'DisplayName', sprintf('Generated Data Set %d', i));
hold on; % Keep the plot hold on for the next data set
end
% Adjust plot settings
grid on;
legend('Location', 'best');
hold off; % Release the plot hold
% Display the generated data for each set
% for i = 1:numSets
% disp(['Generated Data Set ', num2str(i)]);
% %disp(outputData(:,:,i));
% end
% Here is the .xlsx file
dir
% Now read the .xlsx file one sheet by one sheet
for i=1:10
t{i} = readtable(excelFilePath, "sheet", i);
end
t
t{4}
5 Comments
Chunru
on 28 Nov 2023
See below for writing the data into the same sheet (by columns). You need to make the column names unique in this case.
inputData = [0.001 0.1;
0.003 0.1;
0.006 0.1;
0.1 0.09;
0.3 0.08;
0.5 0.07;
5 0.01;
7 0.005;
8 0.001;
10 0.0001];
numSets = 10; % Number of sets to generate
% Initialize an empty matrix to store the generated data
outputData = zeros(size(inputData, 1), 2, numSets);
% Path to the Excel file
excelFilePath = 'generate_sigma.xlsx';
% Generate and write data to separate sheets
Tm =[];
for i = 1:numSets
% Generate similar data with increasing amplitude
scalingFactor = i * 0.1;
generatedData = scalingFactor * inputData(:, 1);
sigma = scalingFactor * inputData(:, 2);
% Store the generated data in the output matrix
outputData(:,:,i) = [generatedData, sigma];
% Create a table for the generated data
Tm = [Tm table(outputData(:,1,i), outputData(:,2,i), 'VariableNames', ["a"+i "b"+i])];
% Write the table to a separate sheet in the Excel file
end
Tm
writetable(Tm, excelFilePath);
t = readtable(excelFilePath)
More Answers (0)
See Also
Categories
Find more on Spreadsheets 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!