www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@xregdesign/gui_import.m
function [d,ok]=gui_import(d) %GUI_IMPORT Import GUI for designs % % [D,OK] = GUI_IMPORT(D) imports data from design files into the design D. % Copyright 2000-2015 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 return function [dout,ok]=i_createfig(d) dlg = mbcgui.container.Dialog('Name','Import Design',... 'HelpCode','xreg_desImport',... 'Size',[360 250],... 'Resize','off'); dlg.Content=i_createlyt(d,dlg.Figure); tg=dlg.showDialog(); if strcmp(tg,'OK') [dout,ok]=i_import(dlg.Content); else dout=d; ok=0; end delete(dlg); return function lyt=i_createlyt(d,figh) udp=xregGui.RunTimePointer; udp.LinkToObject(figh); ud.design=d; frm = mbcgui.container.layoutpanel(... 'Parent', figh, ... 'Title','Import options',... 'BorderType', 'etchedin', ... 'LayoutBorder',[10 0 10 5]); ud.txt1=uicontrol('Parent',figh,... 'Style','text',... 'String','Import from:',... 'HitTest','off',... 'Enable','inactive',... 'HorizontalAlignment','left'); ud.txt2=uicontrol('Parent',frm,... 'Style','text',... 'String','Source file:',... 'HitTest','off',... 'Enable','inactive',... 'HorizontalAlignment','left'); ud.txt3=uicontrol('Parent',frm,... 'Style','text',... 'String','Source variable:',... 'HitTest','off',... 'Enable','inactive',... 'HorizontalAlignment','left'); ud.import=uicontrol('Parent',figh,... 'Style','popupmenu',... 'String',{'Design Editor file (*.mvd)','Comma separated format file (*.csv)', 'Workspace'},... 'Interruptible','off',... 'BackgroundColor','w',... 'Callback',{@i_setopts,udp}); ud.dest=uicontrol('Parent',frm,... 'Style','edit',... 'String','',... 'Interruptible','off',... 'HorizontalAlignment','left'); ud.destname=uicontrol('Parent',frm,... 'Style','edit',... 'String','',... 'Interruptible','off',... 'HorizontalAlignment','left',... 'UserData','',... 'Callback',{@i_setname,udp}); ud.destfile=uicontrol('Parent',frm,... 'Style','pushbutton',... 'String','...',... 'Interruptible','off',... 'Callback',{@i_getfile,udp}); ud.destnamebutton=uicontrol('Parent',frm,... 'Style','pushbutton',... 'String','...',... 'Interruptible','off',... 'Callback',{@i_getvar,udp}); ud.code=uicontrol('Parent',frm,... 'Style','checkbox',... 'Value',0,... 'Enable','off',... 'String','Convert design points from [-1 1] range'); grd=xreggridbaglayout(figh,... 'dimension',[9 4],... 'rowsizes',[3 15 2 5 3 15 2 10 20],... 'colsizes',[85 100 -1 20],... 'gapx',5,... 'mergeblock',{[1 3],[2 3]},... 'mergeblock',{[1 3],[4 4]},... 'mergeblock',{[5 7],[2 3]},... 'mergeblock',{[5 7],[4 4]},... 'mergeblock',{[9 9],[1 4]},... 'elements',{[],ud.dest,[],ud.destfile;... ud.txt2,[],[],[];... [],[],[],[];... [],[],[],[];... [],ud.destname,[],ud.destnamebutton;... ud.txt3,[],[],[];... [],[],[],[];... [],[],[],[];... ud.code,[],[],[]}); set(frm, 'LayoutComponent', {grd}); lyt=xreggridbaglayout(figh,... 'dimension',[5 2],... 'rowsizes',[3 15 2 10 -1],... 'colsizes',[70 -1],... 'mergeblock',{[5 5],[1 2]},... 'mergeblock',{[1 3],[2 2]},... 'elements',{[],ud.txt1,[],[],frm,ud.import},... 'userdata',udp); udp.info=ud; i_doEnable(udp); return function i_doEnable(udp) % set correct enable settings for options ud=udp.info; sc=xregGui.SystemColorsDbl; switch get(ud.import,'Value') case 1 en={'on';'on';'on';'off';'off';'off';'off'}; clr={[1 1 1];sc.CTRL_BACK}; case 2 en={'on';'on';'on';'off';'off';'off';'on'}; clr={[1 1 1];sc.CTRL_BACK}; case 3 en={'off';'off';'off';'on';'on';'on';'on'}; clr={sc.CTRL_BACK;[1 1 1]}; end set([ud.txt2;ud.dest;ud.destfile;ud.txt3;ud.destname;ud.destnamebutton;ud.code],{'Enable'},en); set([ud.dest;ud.destname],{'BackgroundColor'},clr); return function i_setopts(src,evt,udp) % callback from main popup i_doEnable(udp); return function i_getfile(src,evt,udp) % callback from file choosing button ud=udp.info; switch get(ud.import,'Value') case 1 spec='*.mvd'; case 2 spec='*.csv'; end defp = mbcGetPath('mbcmodel', 'Designs', spec); [f,p]=uigetfile(defp); if f~=0 set(ud.dest,'String',fullfile(p,f)); end return function i_getvar(src,evt,udp) ud=udp.info; [unused,ok,s]=mv_getmatrix([NaN,nfactors(ud.design)],'double'); if ok set(ud.destname,'String',s); end return function i_setname(src,evt,udp) % callback from name str=get(src,'String'); ud=udp.info; if isvarname(str) exist= evalin('base',['who(''' str ''')']); if ~isempty(exist) X=evalin('base',str); if isnumeric(X) && size(X,2)==nfactors(ud.design) set(src,'UserData',str); doerror=0; else doerror=1; end else doerror=1; end else doerror=1; end if doerror h=errordlg([str ' does not exist or does not have the correct number of columns.'],'MBC Toolbox','modal'); waitfor(h); set(src,'String',get(src,'UserData')); end return function [d,ok]=i_import(lyt) udp=get(lyt,'UserData'); ud=udp.info; d=ud.design; switch get(ud.import,'Value') case 1 % mvd file str=get(ud.dest,'String'); if ~isempty(str) [d,ok,err]= importfromfile(d,str); else ok=0; err='No filename specified.'; end case 2 % csv file str=get(ud.dest,'String'); if ~isempty(str) [d,ok,err]= importfromcsv(d,get(ud.dest,'String'),get(ud.code,'Value')); else ok=0; err='No filename specified.'; end case 3 % workspace str=get(ud.destname,'String'); if ~isempty(str) d=ud.design; fs=evalin('base',str); realm=model(d); if get(ud.code,'Value') % invcode from [-1 1] space newm = xregCreateModel(@xreglinear,realm); fs=invcode(newm,fs); end % code back to target range for the design object fs=code(realm,fs); % put into design object d=reinit(d,fs,'defined'); ok=1; else ok=0; err='No variable name specified.'; end end if ~ok h=errordlg([sprintf('An error occurred while importing the design:\n') err],'MBC Toolbox','modal'); waitfor(h); end return