plato/simulink/basics.m

71 lines
2.0 KiB
Matlab

clear
% open a simulink system schema1.slx in a new simulink window
open_system('schema1');
% gcs = get current system
% gcs returns the currently open system
assert(isequal(gcs, 'schema1'));
% gcb = get current block
% if a block is clicked inside the schema, gcb returns its value
gcb;
% close the open simulink window
close_system('schema1');
% load_system loads the simulink system without loading the
% interactive simulation interface
% this is faster
system_name = 'schema1';
load_system(system_name);
% simulation is run using the sim function
tic;
sim = sim(system_name);
elapsed = toc; % elapsed simulation time in seconds
% bdroot gives the root node of the current system (gcs)
assert(isequal(bdroot, 'schema1'));
% get_param returns the parameter value associated to a block
% a block can be identified by using its path in the simulink scheme
assert(get_param('schema1/my_gain', 'Gain') == '1'); % <-- the property is a string
% to obtain all parameters
param_struct = get_param('schema1/my_gain', 'ObjectParameters');
assert(length(fieldnames(param_struct)) == 150);
assert(isequal(param_struct.Name.Type, 'string'));
% ----
system2 = 'schema2';
load_system(system2);
% the description of a schema can be obtained using find_system
all_paths = find_system; % returns paths to all open systems
assert(iscell(all_paths));
assert(length(all_paths) == 14);
sys1_paths = find_system(system_name); % find paths relative to schema1
assert(isequal(sys1_paths, ...
{'schema1'; 'schema1/Constant'; 'schema1/Display'; 'schema1/my_gain'}));
% constraint search parameters
my_gain = find_system(system_name, 'Name', 'my_gain');
assert(isequal(get_param(my_gain{1}, 'Gain'), '1'));
% find all blocks of a specific type
all_constants = find_system({system_name, system2}, 'BlockType', 'Constant');
assert(length(all_constants) == 2);
% set_param sets the value of a block parameter
set_param('schema1/my_gain', 'Gain', '10');
assert(isequal(get_param('schema1/my_gain', 'Gain'), '10'));
set_param('schema1/my_gain', 'Gain', '1');
% save system
save_system(system_name);