www.gusucode.com > simulinkcoder 案例源码程序 matlab代码 > simulinkcoder/ConfigureDataInterfaceUsingInportsAndOutportsExample.m

    %% Design Data Interface by Configuring Inport and Outport Blocks
% The data interface of a model is the means by which the model
% exchanges data (for example, signal values) with other, external models or
% systems. Customize the data interface of a model to:
%
% * Enable integration of the generated code with your own code.
% * Improve traceability and readability of the code.
%
% At the top level of a model, Inport and Outport blocks represent
% the input and output signals of the model. To customize the data interface in the generated code, 
% configure these blocks. Early in the design process,
% when a model can contain unconnected Inport and Outport blocks, 
% use this technique to specify the interface before developing the
% internal algorithm.
%
% When you apply storage classes to Inport and Outport blocks, each block
% appears in the generated code as a field of a global structure or as a
% separate global variable that the generated algorithm references
% directly. If you have Embedded Coder, you can use function prototype
% control instead of storage classes to pass data into and out of the model
% |step| function as formal parameters. See <docid:ecoder_ug.bq2nw87-1>.
%% Design Data Interface
% Open the example model <matlab:rtwdemo_basicsc |rtwdemo_basicsc|>.
open_system('rtwdemo_basicsc')
%%
% In the model, select *View > Model Data*.
%
% In the Model Data Editor, select the *Inports/Outports* tab. Each row in
% the table represents an Inport or Outport block.
%
% Name the output signal of the Outport
% block labeled |Out1|. Set *Signal Name* to |output_sig|.
%
% For each of the Inport blocks, set *Data Type* to |single| or to a
% different data type. Due to the data type inheritance settings that the other blocks in the model use by default,
% downstream signals use the same or a similar data type.
%
% Optionally, configure other design attributes
% such as *Min* and *Max* (minimum and maximum values).
%
% Set the *Change View* drop-down list to |Code|.
% 
% For the Outport block and all of the Inport blocks, set *Storage Class* to |ExportedGlobal|. To configure
% all of the blocks at once, select all of the rows in the table.
%
% To configure the blocks and signals, you can use these commands at the
% command prompt.
temp = Simulink.Signal;
temp.StorageClass = 'ExportedGlobal';

portHandles = get_param('rtwdemo_basicsc/In1','portHandles');
outPortHandle = portHandles.Outport;
set_param(outPortHandle,'SignalObject',temp);

portHandles = get_param('rtwdemo_basicsc/In2','portHandles');
outPortHandle = portHandles.Outport;
set_param(outPortHandle,'SignalObject',temp);

portHandles = get_param('rtwdemo_basicsc/In3','portHandles');
outPortHandle = portHandles.Outport;
set_param(outPortHandle,'SignalObject',temp);

portHandles = get_param('rtwdemo_basicsc/In4','portHandles');
outPortHandle = portHandles.Outport;
set_param(outPortHandle,'SignalObject',temp);

set_param('rtwdemo_basicsc/Out1','SignalName','output_sig',...
    'SignalObject',temp)
%%
% Generate code from the model.
rtwbuild('rtwdemo_basicsc');
%%
% View the generated file |rtwdemo_basicsc.c|. Because you applied the
% storage class |ExportedGlobal| to the Inport and Outport blocks, the code
% creates separate
% global variables that represent the inputs and the output.
file = fullfile('rtwdemo_basicsc_grt_rtw','rtwdemo_basicsc.c');
rtwdemodbtype(file,'/* Exported block signals */','real32_T output_sig;',1,1)
%%
% The generated algorithm in the model |step| function directly references these
% global variables to calculate and store the output signal value, |output_sig|.
%
% While you use the Model Data Editor to configure the interface of a
% system, consider using the interface display to view the system inputs
% and outputs (Inport and Outport blocks) at a high level. See
% <docid:simulink_ug.bvcpngd-1>.
%% Route Signal Data to Multiple Outputs
% You can route a single signal to multiple Outport blocks and apply a
% different storage class to each Outport. For example, use this technique
% to send signal data to a custom function (such as a device driver) and to a
% global variable that your custom algorithmic code can use:
%
% # Branch the target signal line to each Outport block.
% # For more efficient code, set the storage class of the target signal line to
% |Auto| (the default). Optimizations can then eliminate the signal line
% from the generated code.
% # Use the Model Data Editor to apply the custom storage class |GetSet| to one Outport block and
% |ExportToFile| to the other Outport block. Apply a signal name to each
% block.
open_system('ex_route_sig')
%% Limitations
% You cannot apply a storage class to an Outport block if the input to the block
% has a variable size. Instead, apply the storage class to the signal line.