How to save the results after 100 times run

2 views (last 30 days)
tao
tao on 21 Aug 2019
Answered: AbioEngineer on 21 Aug 2019
Hello, I need to save the results after 100 times run the code below.
The code runs without error but how can I save the results for "variable L" as excel file output?
Any help will be highly appreciated!
for n = 1: 100;
clc;clear;clf;
lam=1.7;
u=11*42*60/6000;
r1=lam;tr1=60;r2=lam;tr2=0;r3=lam;tr3=0;
s1=3;ts1=60;s2=3;ts2=0;s3=3;ts3=0;
R=[ r1 tr1;
r2 tr2;
r3 tr3];
S=[s1 ts1;
s2 ts2;
s3 ts3];
dddt=[];
rn=length(R(:,1));
for i=1:rn
dt{i}=[];
end
for i=1:rn
while sum(dt{i})<R(i,2)*60
dt{i}=[dt{i} exprnd(R(i,1),1,1)];
end
end
for i=1:rn
dddt=[dddt dt{i}];
end
d=cumsum(dddt);
N=length(d);
wt=zeros(1,N);
mm=zeros(1,s1);
sn=length(S(:,1));
plf=[];
ft=exprnd(u,1,N);
f1=zeros(length(d),s1);
for i=1:s1
f1(i,i)=1;
end
k=0;kk=0;
for i=s1+1:N
llf=[];
lf=zeros(1,s1);
if d(i)>(ts1+ts2)*60
e=find(f1(i-1,:)==2);
for ii=1:N
f1(ii,e)=0;
end
end
for j=1:s1
if max(f1(1:i-1,j))==0
lf(j)=0;
elseif max(f1(1:i-1,j))==1
mm(j)=max(find(f1(1:i-1,j)==1));
lf(j)=d(mm(j))+ft(mm(j))+wt(mm(j));
elseif max(f1(1:i-1,j))==2
lf(j)=inf;
end
if lf(j)==0&&k<s1-s2&&d(i)>ts1*60&&d(i)<(ts1+ts2)*60
k=k+1;
for ii=1:N
f1(ii,j)=2;
end
end
if lf(j)>0&&lf(j)<d(i)&&k<s1-s2&&d(i)>ts1*60&&d(i)<(ts1+ts2)*60
k=k+1;
for ii=1:N
f1(ii,j)=2;
end
end
if lf(j)>d(i)&&k<s1-s2&&d(i)>ts1*60&&d(i)<(ts1+ts2)*60
llf=[llf ;[lf(j) j]];
end
end
if length(llf)~=0
slf=sortrows(llf,1);
if k<s1-s2
for jj=1:s1-s2-k
for ii=1:N
f1(ii,slf(jj,2))=2;kk=kk+1;
end
end
end
k=k+kk;
end
if min(lf)<=d(i)
Tn{i}=find(lf<d(i));
a=randperm(length(Tn{i}));
f1(i,Tn{i}(a(1)))=1;wt(i)=0;
else
b=find(lf(:)==min(lf));
f1(i,b)=1;wt(i)=min(lf)-d(i);
end
fff=f1(i,:);
tlf=[lf f1(i,:) d(i) ft(i) wt(i)];
plf=[plf;tlf];
end
ttts=sort([d'+ft'+wt']);
Td=[[1:N]' d'];Ts=[[1:N]' ttts];
plot(Td(:,2),Td(:,1))
title('arrival/blue+left/red')
xlabel('time/m')
ylabel('number/vehicle')
hold on
plot(Ts(:,2),Ts(:,1),'r')
tss=cumsum(S(:,2)).*60;
for i=1:3
hold on
plot([tss(i) tss(i)],[0 N],'g')
end
axis([0,4000,0,8000])
plot([11*60 11*60],[0 N],'r')
Pwait=mean(wt)
stayti=[d' d'+wt' d'+wt'+ft'];
for t=1:ceil(d(end))
L(t)=0;
for i=1:N
if stayti(i,1)~=stayti(i,2)&&stayti(i,1)<=t&&stayti(i,2)>=t
L(t)=L(t)+1;
end
end
end
PTL=mean(L)
PL(1)=mean(L(1:ts1*60));
PL(2)=mean(L(ts1*60+1:(ts1+ts2)*60));
PL(3)=mean(L((ts1+ts2)*60+1:(ts1+ts2+ts3)*60));
PL
figure(2)
hold on
plot(L)
title('queue length')
xlabel('time/m')
ylabel('number/vihicle')
end

Answers (1)

AbioEngineer
AbioEngineer on 21 Aug 2019
Hi!
There are many things you can do to save as an excel document. It appears you have preset your for loop to run 100 times, so you don't have to add a condition like "if n==100, save the file" or many other options.
If you just want to save a variable: save using save, and load using load. Save filename with the extension .mat
save(filename,variables)
If you want to save an array of things in excel format: after your for loop ends, simply combine the data you wish to export into an array like a cell array for text, or a simple double or float array for numbers.
filename = 'your_filename_here.xlsx';
firstROWtitles = {'title_column_1','title_column_2','title_column_3'}
A = num2cell([1; 2; 3; 4; 5; 6; 7; 8]); % or whatever numbers you want to put
B = cell(8,1); % whatever text you want to put
C = cell(8,1); % even more whatever you want to put
files=[firstROWtitles;[A B C]]
xlswrite(filename,files)

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!