%% Generate Code and Simulate Models in a Project
% This example shows how to use the code generation template for a new Project.
% The code generation project template for Project includes multiple models.
% The project template also provides utilities (.m scripts) that help you generate
% controller code and run simulations of the harness model.
%
% To see the Project API functions for each operation in this
% example, view the example <matlab:edit(fullfile(matlabroot,'examples','simulinkcoder','main','GenerateCodeUsingSmulinkProjectExample.m')) code>.
%
%% Create a Project
% Create a new Project from the code generation project template.
%
% To create this project from the Simulink start page, in the Command Window, type:
%
% |simulink|
%
% Select the Code Generation template from the start page, and create the |exampleCodeGen|
% project.
checkForFolder = exist('./projects');
if (checkForFolder ~= 7)
mkdir projects;
end
cd('projects');
checkForProject = exist('./templateCodeGen/TemplateCodeGenProject.prj');
if (checkForProject ~= 2)
proj_templateCodeGen = Simulink.createFromTemplate('code_generation_example.sltx','Name','templateCodeGenProject','Folder','./templateCodeGen');
else
proj_templateCodeGen = simulinkproject('./templateCodeGen/TemplateCodeGenProject.prj');
end
%% Generate Code
% Generate controller code for the |feedback_control.slx| model.
%
% To generate controller code, select the *Project Shortcuts* tab and select the
% *Generate Controller Code* shortcut.
%
% This shortcut runs the |generate_controller_code.m| script in the |utilities| folder
% of the project. The script builds the |feedback_control.slx| model in the |controller|
% folder of the project.
evalc('run(''../utilities/generate_controller_code.m'')');
%% Simulate Models
% Simulate the top-level harness |feedback_harness.slx| model.
%
% To open the harness model for simulation, select the *Project Shortcuts* tab and
% select the *Feedback Harness* shortcut. This shortcut opens the |feedback_harness.slx|
% model in the |harnesses| folder of the project.
%
% To simulate the model, click *Run*.
open_system('feedback_harness');
sim('feedback_harness');
%% Observe Simulation Output
% Open the |Scope| block in the model and observe the simulation output.
%
% To open the scope and observe simulation, in the Simulink window, double-click the
% |Scope| block.
open_system('feedback_harness/Scope');
%% Use the Dependency Analysis View
% To view file dependencies and to open or run files,
% use the *Dependency Analysis* view of the project.
%
% From the *Dependency Analysis* view, you can:
%
% * Observe file dependencies.
% * Use the context menu for each file to open the file, to run .m script files, and
% run (simulate) model files.
%
% <<../DependencyAnalysisView_templateCodeGenProject.png>>
%
bdclose all;
close(simulinkproject);
clear all;
%% More Information
% * <docid:simulink_ug.bs17ds6-1 Work with Project Files>
% * <docid:simulink_ref.bu4bznn Simulink.createFromTemplate>
% * <docid:simulink_ug.btce77n Automate Startup Tasks>
% * <docid:simulink_ug.btce78j-1 Automate Shutdown Tasks>
%
%% Related Examples
% * <docid:simulink_ug.bsto87f Create a New Project From a Folder>
% * <docid:simulink_ug.buu599v Create a Project from a Model>
%

%%
% Copyright 2012 The MathWorks, Inc.