www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/@mdevtestplan/codegen.m
function codegen(T,Tname) %codegen generate code for test plan and data % Copyright 2014-2015 The MathWorks, Inc. if nargin<2 Tname = matlab.lang.makeValidName(getname(T)); end fid = fopen(sprintf('%s.m',Tname),'wt'); closeFile = onCleanup( @() fclose(fid) ); % function header fprintf(fid,'function T = %s(Project,Data)\n',Tname); fprintf(fid,'%%%s MBC test plan function\n',Tname); fprintf(fid,'%% T = %s(Project,Data);\n',Tname); fprintf(fid,'%% Requires test plan template %s.mbt\n',Tname); fprintf(fid,'%%\n'); v=ver('mbc'); fprintf(fid,'%% Auto-generated from %s in Model-Based Calibration toolbox version %s%s.\n',fullname(T),v.Version,v.Release); fprintf(fid,'\n'); fprintf(fid,'narginchk(2,2)\n'); fprintf(fid,'assert(isa(Project,''mbcmodel.project''),''An mbcmodel.project object is required.'')\n'); fprintf(fid,'\n'); % import data and set up filters, variables etc. codegenData(T,fid) codegenTestplan(T,Tname,fid); function codegenData(T,fid) % import data and set up filters, variables etc. fprintf(fid,'%%Import data into MBC project\n'); fprintf(fid,'D = CreateData(Project,Data);\n'); ssf = info(T.DataLink); X = sweepset(ssf); if size(X,1)~=size(X,3) % define test groups tests = get(ssf,'definetests'); vars = ['{''',strjoin(tests.variable(:)',''','''),'''}']; if isscalar(tests.tolerance) tol = sprintf('%g',tests.tolerance); else % more than one tolerance tol = sprintf('%g,',tests.tolerance); tol = ['[',tol(1:end-1),']']; end if tests.reorder reorder = 'true'; else reorder = 'false'; end if ischar(tests.testnumAlias) testnumAlias = ['''',strtrim(tests.testnumAlias),'''']; else testnumAlias = '0'; end args = strjoin({vars,tol,reorder,testnumAlias},','); fprintf(fid,'%%Test Definition\n'); fprintf(fid,'DefineTestGroups(D,%s);\n',args); end % filters f = get(ssf,'filters'); if ~isempty(f) fprintf(fid,'%%Filters\n'); for i=1:length(f) if f(i).OK fprintf(fid,'AddFilter(D,''%s'');\n',f(i).filterExp); end end end %User variables vars = get(ssf,'variables'); if ~isempty(vars) fprintf(fid,'%%Variables\n'); for i=1:length(vars) if vars(i).OK fprintf(fid,'AddVariable(D,''%s'',''%s'');\n',vars(i).varString,vars(i).varUnit); end end end %Test filters f = get(ssf,'sweepfilters'); if ~isempty(f) fprintf(fid,'%%Test Filters\n'); for i=1:length(f) fprintf(fid,'AddTestFilter(D,''%s'');\n',f(i).filterExp); end end function codegenTestplan(TP,Tname,fid) %generate code for test plan creation, attaching data and building boundary models % create template T = MakeTemplate(TP,false,true,Tname); %#ok<NASGU> save('-mat',[Tname,'.mbt'],'T'); % generate code to create test plan from template and attach data fprintf(fid,'\n'); fprintf(fid,'%%Create test plan and attach data\n'); fprintf(fid,'T = CreateTestplan(Project,''%s.mbt'');\n',Tname); % don't use default boundary models fprintf(fid,'AttachData(T,D,''boundary'',false);\n'); if ~isempty(BoundaryModel(TP)); % create boundary models fprintf(fid,'%%Create boundary models\n'); BTree = boundarytree(TP); if numstages(TP)>1 codegenTwostageBoundary(BTree,fid) else codegenBoundary(BTree,'T.Boundary',fid) end end function codegenTwostageBoundary(BTree,fid) codegenBoundary(BTree.Response,'T.Boundary.Response',fid) codegenBoundary(BTree.Global,'T.Boundary.Global',fid) for i=1:length(BTree.Local.Models) if BTree.Local.InBest(i) mdl = BTree.Local.Models{i}; fprintf(fid,'mdl = CreateBoundary(T.Boundary.Local,''%s'');\n',mdl.Type); % create local boundary model createBoundary(fid,mdl.LocalModel,'mdl.LocalModel','mdl.LocalModel') % add to local boundary tree fprintf(fid,'Add(T.Boundary.Local,mdl)\n'); end end function codegenBoundary(BTree,BTreeString,fid) % onestage for i=1:length(BTree.Models) if BTree.InBest(i) createBoundary(fid,BTree.Models{i},BTreeString,'mdl'); fprintf(fid,'Add(%s,mdl)\n',BTreeString); end end function createBoundary(fid,mdl,mdlCreator,mdlOutput) if ~all(mdl.ActiveInputs) % add active input settings ai = cell(1,mdl.NumberOfInputs); ai(mdl.ActiveInputs) = {'true'}; ai(~mdl.ActiveInputs) = {'false'}; args = sprintf(',''ActiveInputs'',[%s]',strjoin(ai,',')); else args = ''; end fprintf(fid,'%s = CreateBoundary(%s,''%s''%s);\n',mdlOutput,mdlCreator,mdl.Type,args);