image thumbnail
from Design of Mechanism for Function Generator With 5 accuracy point by morteza ahmadi
This is my Mechanism Design project in Mechanical Engineering department of Amkabir University.

mech_export(varargin)
function varargout = mech_export(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @mech_export_OpeningFcn, ...
                   'gui_OutputFcn',  @mech_export_OutputFcn, ...
                   'gui_LayoutFcn',  @mech_export_LayoutFcn, ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end




function mech_export_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
set(handles.error,'visible','off')
set(handles.m_mech,'visible','off')
set(handles.result,'visible','off')
m_fcn_Callback(hObject, eventdata, handles)
clc

disp('  ');
disp('                       ******  Welcome to My program  ****** ');
disp('  ');
disp('      ****** Design of Mechanism for Function Generator With 5 accuracy point ****** ');
disp('  ');
disp('                      **** By : Morteza Ahmadi Najafabadi  ****');
disp('                      **** Mechanism  Design project ****');
disp('  ');
disp('  ');
disp('  ');

function varargout = mech_export_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;



function m_fcn_Callback(hObject, eventdata, handles)

m_f=get(handles.m_fcn,'value')
if(m_f==1)
    set(handles.x_s,'string','1')
    set(handles.x_f,'string','2')
    set(handles.d_fi,'string','60')
    set(handles.d_si,'string','60')
    set(handles.resu,'string','200')
elseif(m_f==2)
    set(handles.x_s,'string','1')
    set(handles.x_f,'string','2')
    set(handles.d_fi,'string','60')
    set(handles.d_si,'string','60')
    set(handles.resu,'string','50')
elseif(m_f==3)
    set(handles.x_s,'string','0')
    set(handles.x_f,'string','1')
    set(handles.d_fi,'string','90')
    set(handles.d_si,'string','90')
    set(handles.resu,'string','25')
end
 axes(handles.m_mech)
 plot(0)
 axes(handles.error)
 plot(0)
set(handles.error,'visible','off')
set(handles.m_mech,'visible','off')
set(handles.result,'visible','off')

function m_fcn_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function x_s_Callback(hObject, eventdata, handles)

function x_s_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function x_f_Callback(hObject, eventdata, handles)

function x_f_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function d_fi_Callback(hObject, eventdata, handles)

function d_fi_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function d_si_Callback(hObject, eventdata, handles)

function d_si_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function start_Callback(hObject, eventdata, handles)

m_f=get(handles.m_fcn,'value')
if(m_f==1)
 
x(1)=str2num(get(handles.x_s,'string'));
x(7)=str2num(get(handles.x_f,'string'));
pd=str2num(get(handles.d_fi,'string'));
qd=str2num(get(handles.d_si,'string'));
P=pd*pi/180;
Q=qd*pi/180;
x1(1)=x(1);   x1(7)=x(7);
for j=2:6
    x1(j)=((x1(1)+x1(7))/2)-(x1(7)-x1(1))/2*cos(pi/10+(pi/5)*(j-2));
end
for j=1:7
    y(j)=sin(x1(j));
end
for j=1:7   
    p(j)=(x1(j)-x1(2))/(x1(7)-x1(1))*P;
    q(j)=(y(j)-y(2))/(y(7)-y(1))*Q;
end
k0=[-1;-1;1;-1;1];
options=optimset('display','off');
[k,fval1,exitflag1]=fsolve(@osys2,k0,options,p,q);
a4=1;  a3=a4/k(1);  a1=a4/k(2);  a2=sqrt(a1^2+a3^2+a4^2-2*a1*a3*k(3));
P1=k(4);   Q1=k(5);
P2=P1+p(1);   Q2=Q1+q(1);
for j=1:101
    X(j)=(x1(7)-x1(1))/100*(j-1)+x1(1);
    f(j)=(X(j)-x1(1))/(x1(7)-x1(1))*P+P2;
    A(j)=sin(f(j));  B(j)=k(2)+cos(f(j));  C(j)=k(1)*cos(f(j))+k(3);
    h(j)=2*atan((A(j)+sqrt(A(j)^2+B(j)^2-C(j)^2))/(B(j)+C(j)));
    Y(j)=(h(j)-Q2)/Q*(y(7)-y(1))+y(1);
    E1(j)=Y(j)-sin(X(j));
end
l=1;  
for j=2:6
    x(j)=((x(1)+x(7))/2)-(x(7)-x(1))/2*cos(pi/10+(pi/5)*(j-2));
end
ll=0;set(handles.pro,'string','0%')
while l>0&ll<str2num(get(handles.resu,'string'))
    ll=ll+1;set(handles.pro,'string',[num2str(round(ll/str2num(get(handles.resu,'string'))*100)) '%'])
    pause(.001)
    for j=1:7
        y(j)=sin(x(j));
    end
    for j=1:7   
        p(j)=(x(j)-x(2))/(x(7)-x(1))*P;
        q(j)=(y(j)-y(2))/(y(7)-y(1))*Q;
    end
    m(2)=0; m(3)=0; m(4)=0; m(5)=0; 
    k0=[-1;-1;1;0;0];
    options=optimset('display','off');
    [k,fval,exitflag]=fsolve(@osys2,k0,options,p,q);
    a4=1;  a3=a4/k(1);  a1=a4/k(2);  a2=sqrt(a1^2+a3^2+a4^2-2*a1*a3*k(3));
    P1=k(4);   Q1=k(5);
    P2=P1+p(1);   Q2=Q1+q(1);
    for j=1:101
        X(j)=(x(7)-x(1))/100*(j-1)+x(1);
        f(j)=(X(j)-x(1))/(x(7)-x(1))*P+P2;
        A(j)=sin(f(j));  B(j)=k(2)+cos(f(j));  C(j)=k(1)*cos(f(j))+k(3);
        h(j)=2*atan((A(j)+sqrt(A(j)^2+B(j)^2-C(j)^2))/(B(j)+C(j)));
        Y(j)=(h(j)-Q2)/Q*(y(7)-y(1))+y(1);
        E(j)=Y(j)-sin(X(j));
        t(j)=abs(E(j));
    end
    for j=1:101
        for r=2:5
            if X(j)>x(r)& X(j)<x(r+1)& t(j)>m(r)
                m(r)=t(j);
            end
        end
    end
    m(1)=t(1);  m(6)=t(101);
    if abs((m(1)-m(2))/m(1))<.25&abs((m(1)-m(3))/m(1))<.25&abs((m(1)-m(4))/m(1))<.25&abs((m(1)-m(5))/m(1))<.25&abs((m(1)-m(6))/m(1))<.25
        l=-1;
    else
        u=0;
        for j=1:6
            if m(j)>u
                u=m(j);
                n=j;
            end
        end
        if n==1
            x(2)=x(2)-(x(7)-x(1))/200;
        elseif n==6
            x(6)=x(6)+(x(7)-x(1))/200;
        else
            x(n)=x(n)+(x(7)-x(1))/400;
            x(n+1)=x(n+1)-(x(7)-x(1))/400;
        end
    end
end
x21=[x1(2) x1(3) x1(4) x1(5) x1(6)];
x22=[x(2) x(3) x(4) x(5) x(6)];
re={'The link lentghs are:';...
    ['a1:      ','a2:      ','a3:      ','a4:   '];...
    [num2str(ceil(100*a1)/100),'    ',num2str(ceil(100*a2)/100),'    ',num2str(ceil(100*a3)/100),'    ',num2str(ceil(100*a4)/100)];...
    'Chebyshef accuracy points :';...
    [num2str(ceil(100*x21(1))/100),'    ',num2str(ceil(100*x21(2))/100),'    ',num2str(ceil(100*x21(3))/100),'    ',num2str(ceil(100*x21(4))/100),'    ',num2str(ceil(100*x21(5))/100)];...
    'Optimum accuracy points:';...
    [num2str(ceil(100*x22(1))/100),'    ',num2str(ceil(100*x22(2))/100),'    ',num2str(ceil(100*x22(3))/100),'    ',num2str(ceil(100*x22(4))/100),'    ',num2str(ceil(100*x22(5))/100)];...
    }

set(handles.result,'string',re')
set(handles.result,'visible','on')
axes(handles.error)
plot(X,E1,'b',X,E,'r');
legend('chebyshef optimum diagram','optimum diagram','Location','best')
xlabel('x');
ylabel('Error');
title('structural error diagram')
grid on;
set(handles.pro,'string','100%')

elseif(m_f==2)%---------------------------------------------------------------------2
x(1)=str2num(get(handles.x_s,'string'));
x(7)=str2num(get(handles.x_f,'string'));
pd=str2num(get(handles.d_fi,'string'));
qd=str2num(get(handles.d_si,'string'));
P=pd*pi/180;
Q=qd*pi/180;
x1(1)=x(1);   x1(7)=x(7);
for j=2:6
    x1(j)=((x1(1)+x1(7))/2)-(x1(7)-x1(1))/2*cos(pi/10+(pi/5)*(j-2));
end
for j=1:7
    y(j)=log10(x1(j));
end
for j=1:7   
    p(j)=(x1(j)-x1(2))/(x1(7)-x1(1))*P;
    q(j)=(y(j)-y(2))/(y(7)-y(1))*Q;
end
k0=[0;0;1;1;1];
options=optimset('display','off');
[k1,fval1,exitflag1]=fsolve(@osys2,k0,options,p,q);
a4=1;  a3=a4/k1(1);  a1=a4/k1(2);  a2=sqrt(a1^2+a3^2+a4^2-2*a1*a3*k1(3));
P1=k1(4);   Q1=k1(5);
P2=P1+p(1);   Q2=Q1+q(1);
for j=1:101
    X(j)=(x1(7)-x1(1))/100*(j-1)+x1(1);
    f(j)=(X(j)-x1(1))/(x1(7)-x1(1))*P+P2;
    A(j)=sin(f(j));  B(j)=k1(2)+cos(f(j));  C(j)=k1(1)*cos(f(j))+k1(3);
    h(j)=2*atan((A(j)+sqrt(A(j)^2+B(j)^2-C(j)^2))/(B(j)+C(j)));
    Y(j)=(h(j)-Q2)/Q*(y(7)-y(1))+y(1);
    E1(j)=Y(j)-log10(X(j));
end
l=1;  
for j=2:6
    x(j)=((x(1)+x(7))/2)-(x(7)-x(1))/2*cos(pi/10+(pi/5)*(j-2));
end
ll=0;set(handles.pro,'string','0%')
while l>0&ll<str2num(get(handles.resu,'string'))
    ll=ll+1;set(handles.pro,'string',[num2str(round(ll/str2num(get(handles.resu,'string'))*100)) '%'])
    pause(.001)
    for j=1:7
        y(j)=log10(x(j));
    end
    for j=1:7   
        p(j)=(x(j)-x(2))/(x(7)-x(1))*P;
        q(j)=(y(j)-y(2))/(y(7)-y(1))*Q;
    end
    m(2)=0; m(3)=0; m(4)=0; m(5)=0; 
    k0=[0;0;0;0;0];
    options=optimset('display','off');
    [k,fval,exitflag]=fsolve(@osys2,k0,options,p,q);
    a4=1;  a3=a4/k(1);  a1=a4/k(2);  a2=sqrt(a1^2+a3^2+a4^2-2*a1*a3*k(3));
    P1=k(4);   Q1=k(5);
    P2=P1+p(1);   Q2=Q1+q(1);
    for j=1:101
        X(j)=(x(7)-x(1))/100*(j-1)+x(1);
        f(j)=(X(j)-x(1))/(x(7)-x(1))*P+P2;
        A(j)=sin(f(j));  B(j)=k(2)+cos(f(j));  C(j)=k(1)*cos(f(j))+k(3);
        h(j)=2*atan((A(j)+sqrt(A(j)^2+B(j)^2-C(j)^2))/(B(j)+C(j)));
        Y(j)=(h(j)-Q2)/Q*(y(7)-y(1))+y(1);
        E(j)=Y(j)-log10(X(j));
        t(j)=abs(E(j));
    end
    for j=1:101
        for r=2:5
            if X(j)>x(r)& X(j)<x(r+1)& t(j)>m(r)
                m(r)=t(j);
            end
        end
    end
    m(1)=t(1);  m(6)=t(101);
    if abs((m(1)-m(2))/m(1))<.2&abs((m(1)-m(3))/m(1))<.2&abs((m(1)-m(4))/m(1))<.2&abs((m(1)-m(5))/m(1))<.2&abs((m(1)-m(6))/m(1))<.2
        l=-1;
    else
        u=0;
        for j=1:6
            if m(j)>u
                u=m(j);
                n=j;
            end
        end
        if n==1
            x(2)=x(2)-(x(7)-x(1))/200;
        elseif n==6
            x(6)=x(6)+(x(7)-x(1))/200;
        else
            x(n)=x(n)+(x(7)-x(1))/400;
            x(n+1)=x(n+1)-(x(7)-x(1))/400;
        end
    end
end
x21=[x1(2) x1(3) x1(4) x1(5) x1(6)];
x22=[x(2) x(3) x(4) x(5) x(6)];
re={'The link lentghs are:';...
    ['a1:      ','a2:      ','a3:      ','a4:   '];...
    [num2str(ceil(100*a1)/100),'    ',num2str(ceil(100*a2)/100),'    ',num2str(ceil(100*a3)/100),'    ',num2str(ceil(100*a4)/100)];...
    'Chebyshef accuracy points :';...
    [num2str(ceil(100*x21(1))/100),'    ',num2str(ceil(100*x21(2))/100),'    ',num2str(ceil(100*x21(3))/100),'    ',num2str(ceil(100*x21(4))/100),'    ',num2str(ceil(100*x21(5))/100)];...
    'Optimum accuracy points:';...
    [num2str(ceil(100*x22(1))/100),'    ',num2str(ceil(100*x22(2))/100),'    ',num2str(ceil(100*x22(3))/100),'    ',num2str(ceil(100*x22(4))/100),'    ',num2str(ceil(100*x22(5))/100)];...
    }
set(handles.result,'string',re')
set(handles.result,'visible','on')
axes(handles.error)
plot(X,E1,'b',X,E,'r');
legend('chebyshef optimum diagram','optimum diagram','Location','best')
xlabel('x');
ylabel('Error');
title('structural error diagram')
grid on;
set(handles.pro,'string','100%')
elseif(m_f==3)%---------------------------------------------------------------------3
syms z;
f=(z)^1.5;
x(1)=str2num(get(handles.x_s,'string'));
x(7)=str2num(get(handles.x_f,'string'));
pd=str2num(get(handles.d_fi,'string'));
qd=str2num(get(handles.d_si,'string'));
Dfi=pd*pi/180;
Dsai=qd*pi/180;
y(1)=subs(f,x(1));
y(7)=subs(f,x(7));
Dx=x(7)-x(1);Dy=y(7)-y(1);
h=.5*(Dx);a=.5*(x(7)+x(1));
for j=2:6
    x(j)=a-h*cos(pi/10+(pi/5)*(j-2));
    y(j)=subs(f,x(j));
end

xx=x(2:6);yy=y(2:6);
for j=2:5  
    fi(j)=(xx(j)-xx(1))/(Dx)*Dfi;
   sai(j)=(yy(j)-yy(1))/(Dy)*Dsai;
end
 k0=[0;0;0;0;0];
 options=optimset('display','off');
[k]=fsolve(@osys1,k0,options,fi,sai);
a1=1/k(2);a3=1/k(1);a2=sqrt((a1^2+a3^2+1)-2*a1*a3*k(3));a4=1
fi1=k(4);sai1=k(5);
fis=fi1-(x(2)-x(1))*Dfi/Dx;fif=fis+Dfi;
sais=sai1-(y(2)-y(1))*Dsai/Dy;saif=sais+Dsai;
X=[linspace(x(1),x(2),50) linspace(x(2),x(3),50) linspace(x(3),x(4),50) linspace(x(4),x(5),50) linspace(x(5),x(6),50) linspace(x(6),x(7),50)];
for i=1:300
    n=Dx/300;
    FI(i)=fis+(X(i)-x(1))*Dfi/Dx;
    A(i)=sin(FI(i));  B(i)=k(2)+cos(FI(i));  C(i)=k(1)*cos(FI(i))+k(3);
    SAI(i)=2*atan((A(i)-sqrt(A(i)^2+B(i)^2-C(i)^2))/(B(i)+C(i)));
    Y(i)=y(1)+(SAI(i)-sais)*Dy /Dsai;
    eror(i)=Y(i)-subs(f,X(i));
end
df=0;
if eror(2)*eror(60)>0
    df=1;
    for i=1:300
        SAI(i)=2*atan((A(i)+sqrt(A(i)^2+B(i)^2-C(i)^2))/(B(i)+C(i)));
        Y(i)=y(1)+(SAI(i)-sais)*Dy /Dsai;
        eror(i)=Y(i)-subs(f,X(i));
    end
end
eror_chebyshev=eror;
X_chebyshev=X;
J=[1 50 100 150 200 250 300];

behineh=0;
ll=0;
while behineh==0&ll<str2num(get(handles.resu,'string'))
    ll=ll+1;
    set(handles.pro,'string',[num2str(round(ll/str2num(get(handles.resu,'string'))*100)) '%'])
    pause(0.001)
for k=1:6
     E(k)= max(abs(eror(J(k):J(k+1))));
end
i_maximum_eror=find(E==max(E));
if i_maximum_eror==1
    x(2)=x(2)-n;
elseif i_maximum_eror==6
    x(6)=x(6)+n;
else
    x(i_maximum_eror+1)=x(i_maximum_eror+1)-n;
    x(i_maximum_eror)=x(i_maximum_eror)+n;
end
i_minimum_eror=find(E==min(E));
if i_minimum_eror==1
    x(2)=x(2)+n;
elseif i_minimum_eror==6
    x(6)=x(6)-n;
else
    x(i_minimum_eror+1)=x(i_minimum_eror+1)+n;
    x(i_minimum_eror)=x(i_minimum_eror)-n;
end

for j=2:6
       y(j)=subs(f,x(j));
end
xx=x(2:6);
yy=y(2:6);
for j=2:5  
    fi(j)=(xx(j)-xx(1))/(Dx)*Dfi;
   sai(j)=(yy(j)-yy(1))/(Dy)*Dsai;
end
 k0=[0;0;0;0;0];
 options=optimset('display','off');
[k]=fsolve(@osys1,k0,options,fi,sai);
a1=1/k(2);
a3=1/k(1);
a2=sqrt((a1^2+a3^2+1)-2*a1*a3*k(3));
a4=1;
fi1=k(4);sai1=k(5);
fis=fi1-(x(2)-x(1))*Dfi/Dx;fif=fis+Dfi;
sais=sai1-(y(2)-y(1))*Dsai/Dy;saif=sais+Dsai;
X=[linspace(x(1),x(2),50) linspace(x(2),x(3),50) linspace(x(3),x(4),50) linspace(x(4),x(5),50) linspace(x(5),x(6),50) linspace(x(6),x(7),50)];
for i=1:300
    FI(i)=fis+(X(i)-x(1))*Dfi/Dx;
    A(i)=sin(FI(i));  B(i)=k(2)+cos(FI(i));  C(i)=k(1)*cos(FI(i))+k(3);
end
if df==0
    for i=1:300
            SAI(i)=2*atan((A(i)-sqrt(A(i)^2+B(i)^2-C(i)^2))/(B(i)+C(i)));
   end
elseif df==1
    for i=1:300
            SAI(i)=2*atan((A(i)+sqrt(A(i)^2+B(i)^2-C(i)^2))/(B(i)+C(i)));
    end
end
for i=1:300
    Y(i)=y(1)+(SAI(i)-sais)*Dy /Dsai;
    eror(i)=Y(i)-subs(f,X(i));
end
if (max(E)-min(E))<=min(E)
    behineh=1;
else
     behineh=0;
end
xx=x(2:6);

end

re={'The link lentghs are:';...
    ['a1:      ','a2:      ','a3:      ','a4:   '];...
    [num2str(ceil(100*a1)/100),'    ',num2str(ceil(100*a2)/100),'    ',num2str(ceil(100*a3)/100),'    ',num2str(ceil(100*a4)/100)];...
    'Optimum accuracy points:';...
    [num2str(ceil(100*x(2))/100),'    ',num2str(ceil(100*x(3))/100),'    ',num2str(ceil(100*x(4))/100),'    ',num2str(ceil(100*x(5))/100),'    ',num2str(ceil(100*x(6))/100)];...
    }
set(handles.result,'string',re')
set(handles.result,'visible','on')
axes(handles.error)

plot(X_chebyshev,eror_chebyshev,'b',X,eror,'r');
legend('chebyshef optimum diagram','optimum diagram','Location','best')
xlabel('x');
ylabel('Error');
title('structural error diagram')
grid on;
set(handles.pro,'string','100%')
P2=fi(2);Q2=sai(2);
end
axes(handles.m_mech)

plot([0 a1*cos(P2)],[0 a1*sin(P2)],'r-o')
hold on
plot([0 -a4],[0 0],'k-o')
plot([-a4 -a4+a3*cos(Q2)],[0 a3*sin(Q2)],'b-o')
plot([-a4+a3*cos(Q2) a1*cos(P2)],[a3*sin(Q2) a1*sin(P2)],'g-o')
axis equal
legend('a1','a4','a3','a2','Location','best') 
hold off
function [F,J]=osys2(k,p,q)
F=[k(1)*cos(k(4)+p(2))-k(2)*cos(k(5)+q(2))+k(3)-cos(k(4)-k(5)+p(2)-q(2));
    k(1)*cos(k(4)+p(3))-k(2)*cos(k(5)+q(3))+k(3)-cos(k(4)-k(5)+p(3)-q(3));
    k(1)*cos(k(4)+p(4))-k(2)*cos(k(5)+q(4))+k(3)-cos(k(4)-k(5)+p(4)-q(4));
    k(1)*cos(k(4)+p(5))-k(2)*cos(k(5)+q(5))+k(3)-cos(k(4)-k(5)+p(5)-q(5));
    k(1)*cos(k(4)+p(6))-k(2)*cos(k(5)+q(6))+k(3)-cos(k(4)-k(5)+p(6)-q(6))];
 if nargout > 1
     J=[cos(k(4)+p(2)),-cos(k(5)+q(2)),1,sin(k(4)-k(5)+p(2)-q(2)),-sin(k(4)-k(5)+p(2)-q(2));
         cos(k(4)+p(3)),-cos(k(5)+q(3)),1,sin(k(4)-k(5)+p(3)-q(3)),-sin(k(4)-k(5)+p(3)-q(3));
         cos(k(4)+p(4)),-cos(k(5)+q(4)),1,sin(k(4)-k(5)+p(4)-q(4)),-sin(k(4)-k(5)+p(4)-q(4));
         cos(k(4)+p(5)),-cos(k(5)+q(5)),1,sin(k(4)-k(5)+p(5)-q(5)),-sin(k(4)-k(5)+p(5)-q(5));
         cos(k(4)+p(6)),-cos(k(5)+q(6)),1,sin(k(4)-k(5)+p(6)-q(6)),-sin(k(4)-k(5)+p(6)-q(6))];
 end
function  F=osys1(k,fi,sai)
F=[k(1)*cos(k(4)+fi(1))-k(2)*cos(k(5)+sai(1))+k(3)-cos(k(4)-k(5)+fi(1)-sai(1));
    k(1)*cos(k(4)+fi(2))-k(2)*cos(k(5)+sai(2))+k(3)-cos(k(4)-k(5)+fi(2)-sai(2));
    k(1)*cos(k(4)+fi(3))-k(2)*cos(k(5)+sai(3))+k(3)-cos(k(4)-k(5)+fi(3)-sai(3));
    k(1)*cos(k(4)+fi(4))-k(2)*cos(k(5)+sai(4))+k(3)-cos(k(4)-k(5)+fi(4)-sai(4));
    k(1)*cos(k(4)+fi(5))-k(2)*cos(k(5)+sai(5))+k(3)-cos(k(4)-k(5)+fi(5)-sai(5))];




function resu_Callback(hObject, eventdata, handles)

function resu_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Creates and returns a handle to the GUI figure. 
function h1 = mech_export_LayoutFcn(policy)
% policy - create a new figure or use a singleton. 'new' or 'reuse'.

persistent hsingleton;
if strcmpi(policy, 'reuse') & ishandle(hsingleton)
    h1 = hsingleton;
    return;
end
load mech_export.mat


appdata = [];
appdata.GUIDEOptions = struct(...
    'active_h', [], ...
    'taginfo', struct(...
    'figure', 2, ...
    'popupmenu', 2, ...
    'text', 10, ...
    'edit', 7, ...
    'axes', 3, ...
    'pushbutton', 2), ...
    'override', 0, ...
    'release', 13, ...
    'resize', 'none', ...
    'accessibility', 'callback', ...
    'mfile', 1, ...
    'callbacks', 1, ...
    'singleton', 1, ...
    'syscolorfig', 1, ...
    'blocking', 0, ...
    'lastSavedFile', 'G:\matlab\mechanismdesign\mechanismdesing\mech_export.m', ...
    'lastFilename', 'G:\matlab\mechanismdesign\mechanismdesing\mech.fig');
appdata.lastValidTag = 'figure1';
appdata.GUIDELayoutEditor = [];
appdata.initTags = struct(...
    'handle', [], ...
    'tag', 'figure1');

h1 = figure(...
'Units','characters',...
'Color',[0.925490196078431 0.913725490196078 0.847058823529412],...
'Colormap',[0 0 0.5625;0 0 0.625;0 0 0.6875;0 0 0.75;0 0 0.8125;0 0 0.875;0 0 0.9375;0 0 1;0 0.0625 1;0 0.125 1;0 0.1875 1;0 0.25 1;0 0.3125 1;0 0.375 1;0 0.4375 1;0 0.5 1;0 0.5625 1;0 0.625 1;0 0.6875 1;0 0.75 1;0 0.8125 1;0 0.875 1;0 0.9375 1;0 1 1;0.0625 1 1;0.125 1 0.9375;0.1875 1 0.875;0.25 1 0.8125;0.3125 1 0.75;0.375 1 0.6875;0.4375 1 0.625;0.5 1 0.5625;0.5625 1 0.5;0.625 1 0.4375;0.6875 1 0.375;0.75 1 0.3125;0.8125 1 0.25;0.875 1 0.1875;0.9375 1 0.125;1 1 0.0625;1 1 0;1 0.9375 0;1 0.875 0;1 0.8125 0;1 0.75 0;1 0.6875 0;1 0.625 0;1 0.5625 0;1 0.5 0;1 0.4375 0;1 0.375 0;1 0.3125 0;1 0.25 0;1 0.1875 0;1 0.125 0;1 0.0625 0;1 0 0;0.9375 0 0;0.875 0 0;0.8125 0 0;0.75 0 0;0.6875 0 0;0.625 0 0;0.5625 0 0],...
'IntegerHandle','off',...
'InvertHardcopy',get(0,'defaultfigureInvertHardcopy'),...
'MenuBar','none',...
'Name','mech',...
'NumberTitle','off',...
'PaperPosition',get(0,'defaultfigurePaperPosition'),...
'Position',[103.8 13.8461538461538 161 47.6153846153846],...
'Resize','off',...
'HandleVisibility','callback',...
'Tag','figure1',...
'UserData',[],...
'Visible','on',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'result';

h2 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',12,...
'HorizontalAlignment','left',...
'Position',[3.6 14.9230769230769 46.4 15.2307692307692],...
'String','result',...
'Style','text',...
'Tag','result',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'text1';

h3 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',12,...
'Position',[8.4 41 10.4 1.38461538461538],...
'String','x(start)',...
'Style','text',...
'Tag','text1',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'text2';

h4 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',12,...
'Position',[8.4 38.9230769230769 10.4 1.38461538461538],...
'String','x(end)',...
'Style','text',...
'Tag','text2',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'text3';

h5 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',12,...
'Position',[8.4 36.7692307692308 10.6 1.61538461538462],...
'String','delta_fi',...
'Style','text',...
'Tag','text3',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'text4';

h6 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',12,...
'Position',[8.4 34.8461538461538 11.4 1.61538461538462],...
'String','delta_si',...
'Style','text',...
'Tag','text4',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'x_s';

h7 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback',mat{1},...
'FontSize',12,...
'Position',[19.8 41 14.2 1.61538461538462],...
'String','1',...
'Style','edit',...
'CreateFcn',mat{2},...
'Tag','x_s');

local_CreateFcn(h7, [], '', appdata);

appdata = [];
appdata.lastValidTag = 'x_f';

h8 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback',mat{3},...
'FontSize',12,...
'Position',[19.8 39 14.2 1.61538461538462],...
'String','2',...
'Style','edit',...
'CreateFcn',mat{4},...
'Tag','x_f');

local_CreateFcn(h8, [], '', appdata);

appdata = [];
appdata.lastValidTag = 'm_mech';

h9 = axes(...
'Parent',h1,...
'Units','characters',...
'Position',[63.8 32.0769230769231 87.8 14.1538461538462],...
'CameraPosition',[0.5 0.5 9.16025403784439],...
'CameraPositionMode',get(0,'defaultaxesCameraPositionMode'),...
'Color',get(0,'defaultaxesColor'),...
'ColorOrder',get(0,'defaultaxesColorOrder'),...
'Layer','top',...
'LooseInset',[14.56 3.55384615384615 10.64 2.42307692307692],...
'XColor',get(0,'defaultaxesXColor'),...
'YColor',get(0,'defaultaxesYColor'),...
'YAxisLocation','right',...
'ZColor',get(0,'defaultaxesZColor'),...
'Tag','m_mech',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

h10 = get(h9,'title');

set(h10,...
'Parent',h9,...
'Units','data',...
'FontUnits','points',...
'BackgroundColor','none',...
'Color',[0 0 0],...
'DisplayName',blanks(0),...
'EdgeColor','none',...
'EraseMode','normal',...
'DVIMode','auto',...
'FontAngle','normal',...
'FontName','Helvetica',...
'FontSize',10,...
'FontWeight','normal',...
'HorizontalAlignment','center',...
'LineStyle','-',...
'LineWidth',0.5,...
'Margin',2,...
'Position',[0.5 1.02989130434783 1.00005459937205],...
'Rotation',0,...
'String',blanks(0),...
'Interpreter','tex',...
'VerticalAlignment','bottom',...
'ButtonDownFcn',[],...
'CreateFcn', {@local_CreateFcn, [], ''} ,...
'DeleteFcn',[],...
'BusyAction','queue',...
'HandleVisibility','off',...
'HelpTopicKey',blanks(0),...
'HitTest','on',...
'Interruptible','on',...
'SelectionHighlight','on',...
'Serializable','on',...
'Tag',blanks(0),...
'UserData',[],...
'Visible','on',...
'XLimInclude','on',...
'YLimInclude','on',...
'ZLimInclude','on',...
'CLimInclude','on',...
'ALimInclude','on',...
'IncludeRenderer','on',...
'Clipping','off');

h11 = get(h9,'xlabel');

set(h11,...
'Parent',h9,...
'Units','data',...
'FontUnits','points',...
'BackgroundColor','none',...
'Color',[0 0 0],...
'DisplayName',blanks(0),...
'EdgeColor','none',...
'EraseMode','normal',...
'DVIMode','auto',...
'FontAngle','normal',...
'FontName','Helvetica',...
'FontSize',10,...
'FontWeight','normal',...
'HorizontalAlignment','center',...
'LineStyle','-',...
'LineWidth',0.5,...
'Margin',2,...
'Position',[0.497722095671982 -0.122282608695652 1.00005459937205],...
'Rotation',0,...
'String',blanks(0),...
'Interpreter','tex',...
'VerticalAlignment','cap',...
'ButtonDownFcn',[],...
'CreateFcn', {@local_CreateFcn, [], ''} ,...
'DeleteFcn',[],...
'BusyAction','queue',...
'HandleVisibility','off',...
'HelpTopicKey',blanks(0),...
'HitTest','on',...
'Interruptible','on',...
'SelectionHighlight','on',...
'Serializable','on',...
'Tag',blanks(0),...
'UserData',[],...
'Visible','on',...
'XLimInclude','on',...
'YLimInclude','on',...
'ZLimInclude','on',...
'CLimInclude','on',...
'ALimInclude','on',...
'IncludeRenderer','on',...
'Clipping','off');

h12 = get(h9,'ylabel');

set(h12,...
'Parent',h9,...
'Units','data',...
'FontUnits','points',...
'BackgroundColor','none',...
'Color',[0 0 0],...
'DisplayName',blanks(0),...
'EdgeColor','none',...
'EraseMode','normal',...
'DVIMode','auto',...
'FontAngle','normal',...
'FontName','Helvetica',...
'FontSize',10,...
'FontWeight','normal',...
'HorizontalAlignment','center',...
'LineStyle','-',...
'LineWidth',0.5,...
'Margin',2,...
'Position',[1.06036446469248 0.491847826086957 1.00005459937205],...
'Rotation',90,...
'String',blanks(0),...
'Interpreter','tex',...
'VerticalAlignment','cap',...
'ButtonDownFcn',[],...
'CreateFcn', {@local_CreateFcn, [], ''} ,...
'DeleteFcn',[],...
'BusyAction','queue',...
'HandleVisibility','off',...
'HelpTopicKey',blanks(0),...
'HitTest','on',...
'Interruptible','on',...
'SelectionHighlight','on',...
'Serializable','on',...
'Tag',blanks(0),...
'UserData',[],...
'Visible','on',...
'XLimInclude','on',...
'YLimInclude','on',...
'ZLimInclude','on',...
'CLimInclude','on',...
'ALimInclude','on',...
'IncludeRenderer','on',...
'Clipping','off');

h13 = get(h9,'zlabel');

set(h13,...
'Parent',h9,...
'Units','data',...
'FontUnits','points',...
'BackgroundColor','none',...
'Color',[0 0 0],...
'DisplayName',blanks(0),...
'EdgeColor','none',...
'EraseMode','normal',...
'DVIMode','auto',...
'FontAngle','normal',...
'FontName','Helvetica',...
'FontSize',10,...
'FontWeight','normal',...
'HorizontalAlignment','right',...
'LineStyle','-',...
'LineWidth',0.5,...
'Margin',2,...
'Position',[-0.727790432801822 1.08967391304348 1.00005459937205],...
'Rotation',0,...
'String',blanks(0),...
'Interpreter','tex',...
'VerticalAlignment','middle',...
'ButtonDownFcn',[],...
'CreateFcn', {@local_CreateFcn, [], ''} ,...
'DeleteFcn',[],...
'BusyAction','queue',...
'HandleVisibility','off',...
'HelpTopicKey',blanks(0),...
'HitTest','on',...
'Interruptible','on',...
'SelectionHighlight','on',...
'Serializable','on',...
'Tag',blanks(0),...
'UserData',[],...
'Visible','off',...
'XLimInclude','on',...
'YLimInclude','on',...
'ZLimInclude','on',...
'CLimInclude','on',...
'ALimInclude','on',...
'IncludeRenderer','on',...
'Clipping','off');

appdata = [];
appdata.lastValidTag = 'd_fi';

h14 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback',mat{5},...
'FontSize',12,...
'Position',[19.8 37 14.2 1.61538461538462],...
'String','60',...
'Style','edit',...
'CreateFcn',mat{6},...
'Tag','d_fi');

local_CreateFcn(h14, [], '', appdata);

appdata = [];
appdata.lastValidTag = 'error';

h15 = axes(...
'Parent',h1,...
'Units','characters',...
'Position',[63 5.07692307692308 90.2 21.2307692307692],...
'CameraPosition',[0.5 0.5 9.16025403784439],...
'CameraPositionMode',get(0,'defaultaxesCameraPositionMode'),...
'Color',get(0,'defaultaxesColor'),...
'ColorOrder',get(0,'defaultaxesColorOrder'),...
'LooseInset',[14.56 3.55384615384615 10.64 2.42307692307692],...
'XColor',get(0,'defaultaxesXColor'),...
'YColor',get(0,'defaultaxesYColor'),...
'ZColor',get(0,'defaultaxesZColor'),...
'Tag','error',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

h16 = get(h15,'title');

set(h16,...
'Parent',h15,...
'Units','data',...
'FontUnits','points',...
'BackgroundColor','none',...
'Color',[0 0 0],...
'DisplayName',blanks(0),...
'EdgeColor','none',...
'EraseMode','normal',...
'DVIMode','auto',...
'FontAngle','normal',...
'FontName','Helvetica',...
'FontSize',10,...
'FontWeight','normal',...
'HorizontalAlignment','center',...
'LineStyle','-',...
'LineWidth',0.5,...
'Margin',2,...
'Position',[0.5 1.01992753623188 1.00005459937205],...
'Rotation',0,...
'String',blanks(0),...
'Interpreter','tex',...
'VerticalAlignment','bottom',...
'ButtonDownFcn',[],...
'CreateFcn', {@local_CreateFcn, [], ''} ,...
'DeleteFcn',[],...
'BusyAction','queue',...
'HandleVisibility','off',...
'HelpTopicKey',blanks(0),...
'HitTest','on',...
'Interruptible','on',...
'SelectionHighlight','on',...
'Serializable','on',...
'Tag',blanks(0),...
'UserData',[],...
'Visible','on',...
'XLimInclude','on',...
'YLimInclude','on',...
'ZLimInclude','on',...
'CLimInclude','on',...
'ALimInclude','on',...
'IncludeRenderer','on',...
'Clipping','off');

h17 = get(h15,'xlabel');

set(h17,...
'Parent',h15,...
'Units','data',...
'FontUnits','points',...
'BackgroundColor','none',...
'Color',[0 0 0],...
'DisplayName',blanks(0),...
'EdgeColor','none',...
'EraseMode','normal',...
'DVIMode','auto',...
'FontAngle','normal',...
'FontName','Helvetica',...
'FontSize',10,...
'FontWeight','normal',...
'HorizontalAlignment','center',...
'LineStyle','-',...
'LineWidth',0.5,...
'Margin',2,...
'Position',[0.497782705099778 -0.0815217391304346 1.00005459937205],...
'Rotation',0,...
'String',blanks(0),...
'Interpreter','tex',...
'VerticalAlignment','cap',...
'ButtonDownFcn',[],...
'CreateFcn', {@local_CreateFcn, [], ''} ,...
'DeleteFcn',[],...
'BusyAction','queue',...
'HandleVisibility','off',...
'HelpTopicKey',blanks(0),...
'HitTest','on',...
'Interruptible','on',...
'SelectionHighlight','on',...
'Serializable','on',...
'Tag',blanks(0),...
'UserData',[],...
'Visible','on',...
'XLimInclude','on',...
'YLimInclude','on',...
'ZLimInclude','on',...
'CLimInclude','on',...
'ALimInclude','on',...
'IncludeRenderer','on',...
'Clipping','off');

h18 = get(h15,'ylabel');

set(h18,...
'Parent',h15,...
'Units','data',...
'FontUnits','points',...
'BackgroundColor','none',...
'Color',[0 0 0],...
'DisplayName',blanks(0),...
'EdgeColor','none',...
'EraseMode','normal',...
'DVIMode','auto',...
'FontAngle','normal',...
'FontName','Helvetica',...
'FontSize',10,...
'FontWeight','normal',...
'HorizontalAlignment','center',...
'LineStyle','-',...
'LineWidth',0.5,...
'Margin',2,...
'Position',[-0.0631929046563193 0.494565217391305 1.00005459937205],...
'Rotation',90,...
'String',blanks(0),...
'Interpreter','tex',...
'VerticalAlignment','bottom',...
'ButtonDownFcn',[],...
'CreateFcn', {@local_CreateFcn, [], ''} ,...
'DeleteFcn',[],...
'BusyAction','queue',...
'HandleVisibility','off',...
'HelpTopicKey',blanks(0),...
'HitTest','on',...
'Interruptible','on',...
'SelectionHighlight','on',...
'Serializable','on',...
'Tag',blanks(0),...
'UserData',[],...
'Visible','on',...
'XLimInclude','on',...
'YLimInclude','on',...
'ZLimInclude','on',...
'CLimInclude','on',...
'ALimInclude','on',...
'IncludeRenderer','on',...
'Clipping','off');

h19 = get(h15,'zlabel');

set(h19,...
'Parent',h15,...
'Units','data',...
'FontUnits','points',...
'BackgroundColor','none',...
'Color',[0 0 0],...
'DisplayName',blanks(0),...
'EdgeColor','none',...
'EraseMode','normal',...
'DVIMode','auto',...
'FontAngle','normal',...
'FontName','Helvetica',...
'FontSize',10,...
'FontWeight','normal',...
'HorizontalAlignment','right',...
'LineStyle','-',...
'LineWidth',0.5,...
'Margin',2,...
'Position',[-0.699556541019956 1.9981884057971 1.00005459937205],...
'Rotation',0,...
'String',blanks(0),...
'Interpreter','tex',...
'VerticalAlignment','middle',...
'ButtonDownFcn',[],...
'CreateFcn', {@local_CreateFcn, [], ''} ,...
'DeleteFcn',[],...
'BusyAction','queue',...
'HandleVisibility','off',...
'HelpTopicKey',blanks(0),...
'HitTest','on',...
'Interruptible','on',...
'SelectionHighlight','on',...
'Serializable','on',...
'Tag',blanks(0),...
'UserData',[],...
'Visible','off',...
'XLimInclude','on',...
'YLimInclude','on',...
'ZLimInclude','on',...
'CLimInclude','on',...
'ALimInclude','on',...
'IncludeRenderer','on',...
'Clipping','off');

appdata = [];
appdata.lastValidTag = 'd_si';

h20 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback',mat{7},...
'FontSize',12,...
'Position',[19.8 35 14.2 1.61538461538462],...
'String','60',...
'Style','edit',...
'CreateFcn',mat{8},...
'Tag','d_si');

local_CreateFcn(h20, [], '', appdata);

appdata = [];
appdata.lastValidTag = 'start';

h21 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback',mat{9},...
'FontSize',12,...
'Position',[12.4 32.0769230769231 21.8 1.76923076923077],...
'String','start',...
'Tag','start',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'text6';

h22 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',12,...
'Position',[6.4 10.6923076923077 16.4 2.23076923076923],...
'String','process :',...
'Style','text',...
'Tag','text6',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'pro';

h23 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',12,...
'Position',[19.8 11.2307692307692 10.8 1.61538461538462],...
'String','0%',...
'Style','text',...
'Tag','pro',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'text8';

h24 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',12,...
'Position',[6.8 8.92307692307692 14.8 1.61538461538462],...
'String','resolution:',...
'Style','text',...
'Tag','text8',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

appdata = [];
appdata.lastValidTag = 'resu';

h25 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback',mat{10},...
'FontSize',12,...
'Position',[21.8 8.53846153846154 7.6 2.07692307692308],...
'String','50',...
'Style','edit',...
'CreateFcn',mat{11},...
'Tag','resu');

local_CreateFcn(h25, [], '', appdata);

appdata = [];
appdata.lastValidTag = 'm_fcn';

h26 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback',mat{12},...
'FontSize',12,...
'Position',[7.8 43.6153846153846 30 2.07692307692308],...
'String',{  'sin(x)'; 'log(x)'; 'x^.5' },...
'Style','popupmenu',...
'Value',1,...
'CreateFcn',mat{13},...
'Tag','m_fcn');

local_CreateFcn(h26, [], '', appdata);


hsingleton = h1;


% --- Set application data first then calling the CreateFcn. 
function local_CreateFcn(hObject, eventdata, createfcn, appdata)

if ~isempty(appdata)
   names = fieldnames(appdata);
   for i=1:length(names)
       name = char(names(i));
       setappdata(hObject, name, getfield(appdata,name));
   end
end

if ~isempty(createfcn)
   eval(createfcn);
end


% --- Handles default GUIDE GUI creation and callback dispatch
function varargout = gui_mainfcn(gui_State, varargin)

gui_StateFields =  {'gui_Name'
    'gui_Singleton'
    'gui_OpeningFcn'
    'gui_OutputFcn'
    'gui_LayoutFcn'
    'gui_Callback'};
gui_Mfile = '';
for i=1:length(gui_StateFields)
    if ~isfield(gui_State, gui_StateFields{i})
        error('MATLAB:gui_mainfcn:FieldNotFound', 'Could not find field %s in the gui_State struct in GUI M-file %s', gui_StateFields{i}, gui_Mfile);
    elseif isequal(gui_StateFields{i}, 'gui_Name')
        gui_Mfile = [gui_State.(gui_StateFields{i}), '.m'];
    end
end

numargin = length(varargin);

if numargin == 0
    % MECH_EXPORT
    % create the GUI only if we are not in the process of loading it
    % already
    gui_Create = true;
elseif local_isInvokeActiveXCallback(gui_State, varargin{:})
    % MECH_EXPORT(ACTIVEX,...)
    vin{1} = gui_State.gui_Name;
    vin{2} = [get(varargin{1}.Peer, 'Tag'), '_', varargin{end}];
    vin{3} = varargin{1};
    vin{4} = varargin{end-1};
    vin{5} = guidata(varargin{1}.Peer);
    feval(vin{:});
    return;
elseif local_isInvokeHGCallbak(gui_State, varargin{:})
    % MECH_EXPORT('CALLBACK',hObject,eventData,handles,...)
    gui_Create = false;
else
    % MECH_EXPORT(...)
    % create the GUI and hand varargin to the openingfcn
    gui_Create = true;
end

if ~gui_Create
    % In design time, we need to mark all components possibly created in
    % the coming callback evaluation as non-serializable. This way, they
    % will not be brought into GUIDE and not be saved in the figure file
    % when running/saving the GUI from GUIDE.
    designEval = false;
    if (numargin>1 && ishghandle(varargin{2}))
        fig = varargin{2};
        while ~isempty(fig) && ~isa(handle(fig),'figure')
            fig = get(fig,'parent');
        end
        
        designEval = isappdata(0,'CreatingGUIDEFigure') || isprop(fig,'__GUIDEFigure');
    end
        
    if designEval
        beforeChildren = findall(fig);
    end
    
    % evaluate the callback now
    varargin{1} = gui_State.gui_Callback;
    if nargout
        [varargout{1:nargout}] = feval(varargin{:});
    else       
        feval(varargin{:});
    end
    
    % Set serializable of objects created in the above callback to off in
    % design time. Need to check whether figure handle is still valid in
    % case the figure is deleted during the callback dispatching.
    if designEval && ishandle(fig)
        set(setdiff(findall(fig),beforeChildren), 'Serializable','off');
    end
else
    if gui_State.gui_Singleton
        gui_SingletonOpt = 'reuse';
    else
        gui_SingletonOpt = 'new';
    end

    % Check user passing 'visible' P/V pair first so that its value can be
    % used by oepnfig to prevent flickering
    gui_Visible = 'auto';
    gui_VisibleInput = '';
    for index=1:2:length(varargin)
        if length(varargin) == index || ~ischar(varargin{index})
            break;
        end

        % Recognize 'visible' P/V pair
        len1 = min(length('visible'),length(varargin{index}));
        len2 = min(length('off'),length(varargin{index+1}));
        if ischar(varargin{index+1}) && strncmpi(varargin{index},'visible',len1) && len2 > 1
            if strncmpi(varargin{index+1},'off',len2)
                gui_Visible = 'invisible';
                gui_VisibleInput = 'off';
            elseif strncmpi(varargin{index+1},'on',len2)
                gui_Visible = 'visible';
                gui_VisibleInput = 'on';
            end
        end
    end
    
    % Open fig file with stored settings.  Note: This executes all component
    % specific CreateFunctions with an empty HANDLES structure.

    
    % Do feval on layout code in m-file if it exists
    gui_Exported = ~isempty(gui_State.gui_LayoutFcn);
    % this application data is used to indicate the running mode of a GUIDE
    % GUI to distinguish it from the design mode of the GUI in GUIDE. it is
    % only used by actxproxy at this time.   
    setappdata(0,genvarname(['OpenGuiWhenRunning_', gui_State.gui_Name]),1);
    if gui_Exported
        gui_hFigure = feval(gui_State.gui_LayoutFcn, gui_SingletonOpt);

        % make figure invisible here so that the visibility of figure is
        % consistent in OpeningFcn in the exported GUI case
        if isempty(gui_VisibleInput)
            gui_VisibleInput = get(gui_hFigure,'Visible');
        end
        set(gui_hFigure,'Visible','off')

        % openfig (called by local_openfig below) does this for guis without
        % the LayoutFcn. Be sure to do it here so guis show up on screen.
        movegui(gui_hFigure,'onscreen');
    else
        gui_hFigure = local_openfig(gui_State.gui_Name, gui_SingletonOpt, gui_Visible);
        % If the figure has InGUIInitialization it was not completely created
        % on the last pass.  Delete this handle and try again.
        if isappdata(gui_hFigure, 'InGUIInitialization')
            delete(gui_hFigure);
            gui_hFigure = local_openfig(gui_State.gui_Name, gui_SingletonOpt, gui_Visible);
        end
    end
    if isappdata(0, genvarname(['OpenGuiWhenRunning_', gui_State.gui_Name]))
        rmappdata(0,genvarname(['OpenGuiWhenRunning_', gui_State.gui_Name]));
    end

    % Set flag to indicate starting GUI initialization
    setappdata(gui_hFigure,'InGUIInitialization',1);

    % Fetch GUIDE Application options
    gui_Options = getappdata(gui_hFigure,'GUIDEOptions');
    % Singleton setting in the GUI M-file takes priority if different
    gui_Options.singleton = gui_State.gui_Singleton;

    if ~isappdata(gui_hFigure,'GUIOnScreen')
        % Adjust background color
        if gui_Options.syscolorfig
            set(gui_hFigure,'Color', get(0,'DefaultUicontrolBackgroundColor'));
        end

        % Generate HANDLES structure and store with GUIDATA. If there is
        % user set GUI data already, keep that also.
        data = guidata(gui_hFigure);
        handles = guihandles(gui_hFigure);
        if ~isempty(handles)
            if isempty(data)
                data = handles;
            else
                names = fieldnames(handles);
                for k=1:length(names)
                    data.(char(names(k)))=handles.(char(names(k)));
                end
            end
        end
        guidata(gui_hFigure, data);
    end

    % Apply input P/V pairs other than 'visible'
    for index=1:2:length(varargin)
        if length(varargin) == index || ~ischar(varargin{index})
            break;
        end

        len1 = min(length('visible'),length(varargin{index}));
        if ~strncmpi(varargin{index},'visible',len1)
            try set(gui_hFigure, varargin{index}, varargin{index+1}), catch break, end
        end
    end

    % If handle visibility is set to 'callback', turn it on until finished
    % with OpeningFcn
    gui_HandleVisibility = get(gui_hFigure,'HandleVisibility');
    if strcmp(gui_HandleVisibility, 'callback')
        set(gui_hFigure,'HandleVisibility', 'on');
    end

    feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});

    if isscalar(gui_hFigure) && ishandle(gui_hFigure)
        % Handle the default callbacks of predefined toolbar tools in this
        % GUI, if any
        guidemfile('restoreToolbarToolPredefinedCallback',gui_hFigure); 
        
        % Update handle visibility
        set(gui_hFigure,'HandleVisibility', gui_HandleVisibility);

        % Call openfig again to pick up the saved visibility or apply the
        % one passed in from the P/V pairs
        if ~gui_Exported
            gui_hFigure = local_openfig(gui_State.gui_Name, 'reuse',gui_Visible);
        elseif ~isempty(gui_VisibleInput)
            set(gui_hFigure,'Visible',gui_VisibleInput);
        end
        if strcmpi(get(gui_hFigure, 'Visible'), 'on')
            figure(gui_hFigure);
            
            if gui_Options.singleton
                setappdata(gui_hFigure,'GUIOnScreen', 1);
            end
        end

        % Done with GUI initialization
        if isappdata(gui_hFigure,'InGUIInitialization')
            rmappdata(gui_hFigure,'InGUIInitialization');
        end

        % If handle visibility is set to 'callback', turn it on until
        % finished with OutputFcn
        gui_HandleVisibility = get(gui_hFigure,'HandleVisibility');
        if strcmp(gui_HandleVisibility, 'callback')
            set(gui_hFigure,'HandleVisibility', 'on');
        end
        gui_Handles = guidata(gui_hFigure);
    else
        gui_Handles = [];
    end

    if nargout
        [varargout{1:nargout}] = feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
    else
        feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
    end

    if isscalar(gui_hFigure) && ishandle(gui_hFigure)
        set(gui_hFigure,'HandleVisibility', gui_HandleVisibility);
    end
end

function gui_hFigure = local_openfig(name, singleton, visible)

% openfig with three arguments was new from R13. Try to call that first, if
% failed, try the old openfig.
if nargin('openfig') == 2
    % OPENFIG did not accept 3rd input argument until R13,
    % toggle default figure visible to prevent the figure
    % from showing up too soon.
    gui_OldDefaultVisible = get(0,'defaultFigureVisible');
    set(0,'defaultFigureVisible','off');
    gui_hFigure = openfig(name, singleton);
    set(0,'defaultFigureVisible',gui_OldDefaultVisible);
else
    gui_hFigure = openfig(name, singleton, visible);
end

function result = local_isInvokeActiveXCallback(gui_State, varargin)

try
    result = ispc && iscom(varargin{1}) ...
             && isequal(varargin{1},gcbo);
catch
    result = false;
end

function result = local_isInvokeHGCallbak(gui_State, varargin)

try
    fhandle = functions(gui_State.gui_Callback);
    result = ~isempty(findstr(gui_State.gui_Name,fhandle.file)) || ...
             (ischar(varargin{1}) ...
             && isequal(ishandle(varargin{2}), 1) ...
             && (~isempty(strfind(varargin{1},[get(varargin{2}, 'Tag'), '_'])) || ...
                ~isempty(strfind(varargin{1}, '_CreateFcn'))) );
catch
    result = false;
end


Contact us at files@mathworks.com