www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@xregdesign/gui_export.m
function [d,ok]=gui_export(d,varargin) %GUI_EXPORT Export GUI for designs % % GUI_EXPORT(D) % Copyright 2000-2016 The MathWorks, Inc. and Ford Global Technologies, Inc. if nargin<2 action='figure'; end switch lower(action) case 'figure' [d,ok]=i_createfig(d); end function [dout,ok]=i_createfig(d) dlg=mbcgui.container.Dialog('Name','Export Design',... 'Size',[360 250],... 'HelpCode','xreg_desExport',... 'Resize','off'); figh=dlg.Figure; dlg.Content=i_createlyt(d,figh); tg=dlg.showDialog(); if strcmp(tg,'OK') i_export(dlg.Content); ok=1; else ok=0; end dout=d; delete(dlg); function lyt=i_createlyt(d,figh) udp=xregGui.RunTimePointer; udp.LinkToObject(figh); sc=xregGui.SystemColorsDbl; ud.exportTypes = struct('String', {'Comma separated format file (*.csv)', 'Design Editor file (*.mvd)', 'Workspace'}, ... 'Ext', {'csv', 'mvd', 'workspace'}, ... 'IsExportFile', {true, true, false}, ... 'enable', {{'on';'on';'on';'off';'off';'on';'on'}, {'on';'on';'on';'off';'off';'off';'off'}, {'off';'off';'off';'on';'on';'on';'off'}}, ... 'color', {{[1 1 1];sc.CTRL_BACK}, {[1 1 1];sc.CTRL_BACK}, {sc.CTRL_BACK;[1 1 1]}}); ud.design=d; frm = mbcgui.container.layoutpanel(... 'Parent', figh, ... 'Title','Export options',... 'BorderType', 'etchedin', ... 'LayoutBorder',[10 0 10 5]); ud.txt1=uicontrol('Parent',figh,... 'Style','text',... 'String','Export to:',... 'HitTest','off',... 'Enable','inactive',... 'HorizontalAlignment','left'); ud.txt2=uicontrol('Parent',frm,... 'Style','text',... 'String','Destination file:',... 'HitTest','off',... 'Enable','inactive',... 'HorizontalAlignment','left'); ud.txt3=uicontrol('Parent',frm,... 'Style','text',... 'String','Export as:',... 'HitTest','off',... 'Enable','inactive',... 'HorizontalAlignment','left'); ud.export=uicontrol('Parent',figh,... 'Style','popupmenu',... 'String',{ud.exportTypes.String},... 'Interruptible','off',... 'BackgroundColor','w',... 'Tag', 'ExportType', ... 'Callback',{@i_setopts,udp}); ud.dest=uicontrol('Parent',frm,... 'Style','edit',... 'String','',... 'Interruptible','off',... 'Tag', 'DestFile', ... 'HorizontalAlignment','left'); ud.destname=uicontrol('Parent',frm,... 'Style','edit',... 'String','',... 'Interruptible','off',... 'HorizontalAlignment','left',... 'Tag', 'DestName', ... 'Callback',{@i_setname}); ud.destfile=uicontrol('Parent',frm,... 'Style','pushbutton',... 'String','...',... 'Interruptible','off',... 'Tag', 'FileChooser', ... 'Callback',{@i_getfile,udp}); ud.code=uicontrol('Parent',frm,... 'Style','checkbox',... 'Value',0,... 'Enable','off',... 'Tag', 'NormaliseRangeCheckbox', ... 'String','Convert design points to [-1 1] range'); ud.symb=uicontrol('Parent',frm,... 'Style','checkbox',... 'Value',1,... 'Enable','off',... 'Tag', 'IncludeFactorsCheckbox', ... 'String','Include factor symbols'); grd=xreggridbaglayout(frm,... 'packstatus','off',... 'dimension',[11 4],... 'rowsizes',[3 15 2 5 3 15 2 10 20 5 20],... 'colsizes',[85 100 -1 20],... 'gapx',5,... 'mergeblock',{[1 3],[2 3]},... 'mergeblock',{[1 3],[4 4]},... 'mergeblock',{[5 7],[2 2]},... 'mergeblock',{[9 9],[1 4]},... 'mergeblock',{[11 11],[1 4]},... 'elements',{[],ud.dest,[],ud.destfile;... ud.txt2,[],[],[];... [],[],[],[];... [],[],[],[];... [],ud.destname,[],[];... ud.txt3,[],[],[];... [],[],[],[];... [],[],[],[];... ud.code,[],[],[];... [],[],[],[];... ud.symb,[],[],[]}); set(frm, 'LayoutComponent', {grd}); lyt=xreggridbaglayout(figh,... 'dimension',[5 2],... 'rowsizes',[3 15 2 10 -1],... 'colsizes',[60 -1],... 'mergeblock',{[5 5],[1 2]},... 'mergeblock',{[1 3],[2 2]},... 'elements',{[],ud.txt1,[],[],frm,ud.export},... 'userdata',udp); udp.info=ud; i_setvalues(udp) i_doEnable(udp); function i_setvalues(udp) ud=udp.info; exportTypes = ud.exportTypes; set([ud.dest;ud.destname],{'String'},{ [name(ud.design) '.' exportTypes(1).Ext] mbcMakeValidName( name(ud.design),'Prefix', 'd_') }); set(ud.destname,'UserData',get(ud.destname,'String')); function i_setopts(src,evt,udp) % callback from main popup i_doEnable(udp); i_doName(udp); function i_doEnable(udp) % set correct enable settings for options ud=udp.info; exportTypes = ud.exportTypes; selectedIndex = get(ud.export,'Value'); en = exportTypes(selectedIndex).enable; clr = exportTypes(selectedIndex).color; set([ud.txt2;ud.dest;ud.destfile;ud.txt3;ud.destname;ud.code;ud.symb],{'Enable'},en); set([ud.dest;ud.destname],{'BackgroundColor'},clr); function i_doName(udp) %i_doName Set the name of the exported file ud=udp.info; exportTypes = ud.exportTypes; str=get(ud.dest,'String'); val=get(ud.export,'Value'); if exportTypes(val).IsExportFile [path,filename]=fileparts(str); str=fullfile(path,[filename '.' exportTypes(val).Ext]); set(ud.dest,'String',str); end function i_getfile(src,evt,udp) % callback from file choosing button ud=udp.info; exportTypes = ud.exportTypes; val = get(ud.export,'Value'); spec = ['*.' exportTypes(val).Ext]; [f,p]=uiputfile(mbcGetPath('mbcmodel', 'Designs', spec)); if f~=0 [~,~,ext]=fileparts(f); if isempty(ext) f=[f '.' exportTypes(val).Ext]; end set(ud.dest,'String',fullfile(p,f)); end function i_setname(src,evt) % callback from name str=get(src,'String'); if isvarname(str) set(src,'UserData',str); else h=errordlg([str ' is not a valid MATLAB name.'],'MBC Toolbox','modal'); waitfor(h); set(src,'String',get(src,'UserData')); end function i_export(lyt) %i_export Do the export to the selected type udp = get(lyt,'UserData'); ud = udp.info; d = ud.design; exportTypes = ud.exportTypes; val = get(ud.export,'Value'); switch exportTypes(val).Ext case 'mvd' % mvd file [ok,err] = exporttofile(d,get(ud.dest,'String')); case 'csv' % csv file [ok,err] = exporttocsv(d,get(ud.dest,'String'),get(ud.code,'Value'),get(ud.symb,'Value')); case 'workspace' % workspace fs = invcode(model(d),factorsettings(d)); if get(ud.code,'Value') % code to [-1 1] space realm = model(d); newm = xregCreateModel(@xreglinear,realm); fs = code(newm,fs); end assignin('base',get(ud.destname,'String'),fs); ok = 1; end if ~ok h = errordlg(sprintf('An error occurred while exporting the design:\n%s', err),'MBC Toolbox','modal'); drawnow; waitfor(h); end