function guide2m(fighand,fname);
%guide2m.m  converts a figure to a simple function
%
%guide2m(fighand,fname);
%
%Creates an m-file that reproduces the position, types, and values
%of the objects contained in fighand
%

fid=fopen(fname,'wt');%mfile for output
if(fid<1)
    error(['unable to open ',fname]);
end

obj=get(fighand,'children');
n=length(obj);

set(fighand,'units','normalized');

str=['function ',fname(1:end-2),];
fprintf(fid,[str,'\n']);
fprintf(fid,['%%',fname,'\n']);
fprintf(fid,'%%Created by guide2.m\n');

fprintf(fid,'\n');
fprintf(fid,'fig=figure; %%create a figure\n');
pos=get(fighand,'position');
fprintf(fid,['set(fig,''units'',''normalized'',''position'',[',num2str(pos),']);\n']);
fprintf(fid,'\n');

for j=1:n;
    set(obj(j),'units','normalized');
    pos=get(obj(j),'position');
    type=get(obj(j),'type');
    fprintf(fid,['h(',int2str(j),')=',type,'(''units'',''normalized'',''position'',[',num2str(pos),'],...\n']);
    if(lower(type)=='uicontrol')
        sty=get(obj(j),'style');
        fprintf(fid,['    ''style'',''',sty,''',...\n']);
        val=get(obj(j),'value');
        if(ischar(val))
            fprintf(fid,['    ''value'',''',val,''',...\n']);
        else
            fprintf(fid,['    ''value'',',num2str(val),',...\n']);
        end            
    end
    tag=get(obj(j),'tag');
    fprintf(fid,[     '''tag'',''',tag,''');\n']);
    fprintf(fid,'\n');
end
fclose(fid);