我使用while循环,我的项目没有响应(MATLAB)

时间:2013-05-24 14:44:29

标签: matlab function while-loop

我是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);

我的项目将使用多个公式计算统计数据。所以,我在“功能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);
        ....

所以,我只尝试了一个列表框,只要程序没有使用while循环,它就能正常工作。

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

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

2 个答案:

答案 0 :(得分:3)

我看到你有这个循环:

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

答案 1 :(得分:0)

丹尼斯是对的。您可能想要的另一个猜测是确保q得到更新。如你所知,q设置在while循环之外,永远不会在内部发生变化。

因为你有

tt= q*60;
需要在循环内更新

q,以便同时tt