save multiple random numbers on different sheet of execl
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
abdul rehman
el 21 de Dic. de 2021
Respondida: Walter Roberson
el 21 de Dic. de 2021
%% I want to replace data with data1, actually want to controll random number for each column in every sheet
VariableNames = {'Customer','x','y','d'};
SheetNames = {'A','B','C','D'};
filename_out = 'test.xlsx';
for hh = 1:numel(SheetNames)
% export to excel
data = rand(100,numel(VariableNames));
%%data1 =[rand(100,1),randi([0,1],100,1),randi([5,8],100,1),randi([5,8],100,1),randi([5,12],100,1)]
writecell(VariableNames,filename_out,"Sheet" ,char(SheetNames(hh)),"Range",'A1'); % save column headers in first line
writematrix(data,filename_out,"Sheet" ,char(SheetNames(hh)),"Range",'A2'); % save data (cell) to excel file
end
2 comentarios
Walter Roberson
el 21 de Dic. de 2021
Is there a particular reason you are not creating a table and using writetable() ?
T = array2table(data, 'VariableNames', VariableNames);
writetable(T, filename_out, 'Sheet', SheetNames{hh});
Respuesta aceptada
Walter Roberson
el 21 de Dic. de 2021
VariableNames = {'Customer', 'x', 'y', 'd', 'jolene'};
SheetNames = {'A','B','C','D'};
filename_out = 'test.xlsx';
for hh = 1:numel(SheetNames)
data = table(rand(100,1),randi([0,1],100,1),randi([5,8],100,1),randi([5,8],100,1),randi([5,12],100,1), 'VariableNames', VariableNames);
writetable(data, filename_out, "Sheet", SheetNames{hh}); % save data to excel file
end
%id it work?
readback = readtable(filename_out, "Sheet", SheetNames{end});
readback(1:5,:)
data(1:5,:)
data{1:5,:} - readback{1:5,:}
%yup!
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!