save multiple random numbers on different sheet of execl

조회 수: 1 (최근 30일)
abdul rehman
abdul rehman 2021년 12월 21일
답변: Walter Roberson 2021년 12월 21일
%% 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
Walter Roberson
Walter Roberson 2021년 12월 21일
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});
abdul rehman
abdul rehman 2021년 12월 21일
No I just want to save multiple excel sheets with different random patterns in different columns, if it can be done any easy way

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 12월 21일
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,:)
ans = 5×5 table
Customer x y d jolene ________ _ _ _ ______ 0.63969 0 5 7 9 0.11981 1 7 7 11 0.88455 1 7 8 5 0.70752 1 6 8 6 0.44112 0 7 6 9
data(1:5,:)
ans = 5×5 table
Customer x y d jolene ________ _ _ _ ______ 0.63969 0 5 7 9 0.11981 1 7 7 11 0.88455 1 7 8 5 0.70752 1 6 8 6 0.44112 0 7 6 9
data{1:5,:} - readback{1:5,:}
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
%yup!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by