0

我是 MATLAB 编程的初学者。我正在处理统计数据,我在循环时遇到了一些问题。这是代码:

% --- Executes on button press in b_analisa_data.
function b_analisa_data_Callback(hObject, eventdata, handles)
hitung(handles.lb_tkp, handles);
tkp=getappdata(handles.lb_tkp,'tkp2');
tpp=getappdata(handles.lb_tpp,'tpp2');
po=getappdata(handles.lb_po,'po2');
nq=getappdata(handles.lb_nq,'nq2');
nt=getappdata(handles.lb_nt,'nt2');
tq=getappdata(handles.lb_tq,'tq2');
tt=getappdata(handles.lb_tt,'tt2');
s=getappdata(handles.lb_s,'s2');

set(handles.lb_tkp,'String',tkp);
set(handles.lb_tpp,'String',tpp);
set(handles.lb_po,'String',po);
set(handles.lb_nq,'String',nq);
set(handles.lb_nt,'String',nt);
set(handles.lb_tq,'String',tq);
set(handles.lb_tt,'String',tt);
set(handles.lb_s,'String',s);
% hObject    handle to b_analisa_data (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


function hitung(handles_hitung,handles)
% Perhitungan tingkat kedatangan pelanggan (TKP)
a=str2num(get(handles.st_twak,'String'));
b=str2num(get(handles.st_jwak,'String'));
param=str2num(get(handles.txt_param,'String'));
c=a/b;
t=3600;
tkp=t/c;
%set(handles.lb_tkp,'String',tkp);

% Perhitungan tingkat pelayanan pelanggan (TPP)
d=str2num(get(handles.st_twp,'String'));
e=str2num(get(handles.st_jwp,'String'));
f=d/e;
tpp=t/f;
%set(handles.lb_tpp,'String',tpp);

% Perhitungan probabilitas tidak adanya pelanggan (PO)
s=str2num(get(handles.st_kasir,'String'));
g=tkp/tpp;
h=tkp/(s*tpp);
for i=0:s-1;
    j(i+1)=(g^i)/(factorial(i));
end
k=sum(j);
l=g/((factorial(s)*(1-h)));
m=k+l;
po=1/m;
%set(handles.lb_po,'String',po);

% Perhitungan jumlah pelanggan dalam antrian (NQ)
n=po*(g^s)*h;
o=factorial(s);
p=o*((1-h)^2);
nq=n/p;
%set(handles.lb_nq,'String',nq);

% Perhitungan jumlah pelanggan dalam sistem (NT)
nt=nq+g;
%set(handles.lb_nt,'String',nt);

% Perhitungan waktu tunggu rata-rata dalam antrian (TQ)
tq=nq/tkp;
%set(handles.lb_tq,'String',tq);

% Perhitungan waktu tunggu rata-rata dalam sistem (TT)
q=tq+(1/tpp);
tt= q*60;
%set(handles.lb_tt,'String',tt);

% Perhitungan jumlah kasir (S)
%set(handles.lb_s,'String',s);

while tt>param
    s=s-1;
    tkp=t/c;
    tpp=t/f;
    po=1/m;
    nq=n/p;
    nt=nq+g;
    tq=nq/tkp;
    tt= q*60;
end
setappdata(handles_hitung,'tkp2',tkp);
setappdata(handles_hitung,'tpp2',tpp);
setappdata(handles_hitung,'po2',po);
setappdata(handles_hitung,'nq2',nq);
setappdata(handles_hitung,'nt2',nt);
setappdata(handles_hitung,'tq2',tq);
setappdata(handles_hitung,'tt2',tt);
setappdata(handles_hitung,'s2',s);

我的项目将使用多个公式计算统计数据。所以,我把所有公式都放在“function hitung”中

function hitung(handles_hitung,handles)
% Perhitungan tingkat kedatangan pelanggan (TKP)
a=str2num(get(handles.st_twak,'String'));
b=str2num(get(handles.st_jwak,'String'));
param=str2num(get(handles.txt_param,'String'));
c=a/b;
t=3600;
tkp=t/c;
%set(handles.lb_tkp,'String',tkp);

% Perhitungan tingkat pelayanan pelanggan (TPP)
d=str2num(get(handles.st_twp,'String'));
e=str2num(get(handles.st_jwp,'String'));
f=d/e;
tpp=t/f;
%set(handles.lb_tpp,'String',tpp);

% Perhitungan probabilitas tidak adanya pelanggan (PO)
s=str2num(get(handles.st_kasir,'String'));
g=tkp/tpp;
h=tkp/(s*tpp);
for i=0:s-1;
    j(i+1)=(g^i)/(factorial(i));
end
k=sum(j);
l=g/((factorial(s)*(1-h)));
m=k+l;
po=1/m;
%set(handles.lb_po,'String',po);

% Perhitungan jumlah pelanggan dalam antrian (NQ)
n=po*(g^s)*h;
o=factorial(s);
p=o*((1-h)^2);
nq=n/p;
%set(handles.lb_nq,'String',nq);

% Perhitungan jumlah pelanggan dalam sistem (NT)
nt=nq+g;
%set(handles.lb_nt,'String',nt);

% Perhitungan waktu tunggu rata-rata dalam antrian (TQ)
tq=nq/tkp;
%set(handles.lb_tq,'String',tq);

% Perhitungan waktu tunggu rata-rata dalam sistem (TT)
q=tq+(1/tpp);
tt= q*60;
%set(handles.lb_tt,'String',tt);

% Perhitungan jumlah kasir (S)
%set(handles.lb_s,'String',s);

while tt>param
    s=s-1;
    tkp=t/c;
    tpp=t/f;
    po=1/m;
    nq=n/p;
    nt=nq+g;
    tq=nq/tkp;
    tt= q*60;
end
setappdata(handles_hitung,'tkp2',tkp);
setappdata(handles_hitung,'tpp2',tpp);
setappdata(handles_hitung,'po2',po);
setappdata(handles_hitung,'nq2',nq);
setappdata(handles_hitung,'nt2',nt);
setappdata(handles_hitung,'tq2',tq);
setappdata(handles_hitung,'tt2',tt);
setappdata(handles_hitung,'s2',s);

我在“function b_analisa_data Callback”中调用“function hitung”

function b_analisa_data_Callback(hObject, eventdata, handles)
    hitung(handles.lb_tkp, handles);
    tkp=getappdata(handles.lb_tkp,'tkp2');
    tpp=getappdata(handles.lb_tpp,'tpp2');
    po=getappdata(handles.lb_po,'po2');
    nq=getappdata(handles.lb_nq,'nq2');
    nt=getappdata(handles.lb_nt,'nt2');
    tq=getappdata(handles.lb_tq,'tq2');
    tt=getappdata(handles.lb_tt,'tt2');
    s=getappdata(handles.lb_s,'s2');

    set(handles.lb_tkp,'String',tkp);
    set(handles.lb_tpp,'String',tpp);
    set(handles.lb_po,'String',po);
    set(handles.lb_nq,'String',nq);
    set(handles.lb_nt,'String',nt);
    set(handles.lb_tq,'String',tq);
    set(handles.lb_tt,'String',tt);
    set(handles.lb_s,'String',s);
    % hObject    handle to b_analisa_data (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)

我的程序将显示计算 8 个列表框中的公式的所有结果。但我不知道调用“函数 hitung”,所以它也可以显示在 8 个列表框中。我尝试了下面的代码并得到了一些错误:

function b_analisa_data_Callback(hObject, eventdata, handles)
        hitung(handles.lb_tkp,handles.lb_tpp, handles);
        ....

所以,我只尝试了 1 个列表框,只要程序不使用 while 循环,它就可以工作。

function b_analisa_data_Callback(hObject, eventdata, handles)
        hitung(handles.lb_tkp, handles);
        ...

如果条件使程序使用 while 循环,则它变得没有响应。我仍然很困惑为什么会变成这样。

4

2 回答 2

3

我看到你有这个while循环:

while tt>param
    s=s-1;
    tkp=t/c;
    tpp=t/f;
    po=1/m;
    nq=n/p;
    nt=nq+g;
    tq=nq/tkp;
    tt= q*60;
end

两者ttparam永远不会改变(tt第一次可以改变)。因此,一旦您进入第二次迭代,您就永远无法退出。


一些你可能想要的猜测,如果它不仅仅是一个错字:

  1. 如果您只想这样做一次,while请将if
  2. 如果要等到变量更新,则需要将更新此变量所需的所有内容放入循环中。(例如getappdata
于 2013-05-24T14:51:50.003 回答
0

丹尼斯是对的。您可能想要的另一个猜测是确保q得到更新。正如您所拥有的,它q设置在 while 循环之外,并且永远不会在内部更改。

既然你有

tt= q*60;

q需要在循环内更新才能更新tt

于 2013-05-24T15:08:07.483 回答