Friday, February 10, 2017

[Matlab / Simulink] How to input a value to the constant block of Simulink using m script.

1. Syntax
 set_param( 'model name/subsystem name/block name' , 'Value' , 'number' )

2. Simulink Model
- Model name : Notebook
- Subsystem : Blog
- Constant block : speed

3. Command
set_param('Notebook/Blog/speed', 'Value', '3')

With the above command, a value of a specified constant block will be changed to 3.

4. Application
It might be useful when changing or entering hundreds of values of the constant blocks.

% model name
model = 'Notebook/';
% subsystem name
sub = 'Blog/'
% block names
block = { 'spd1'; 'spd2'; 'spd3'};
% values
val = [2; 3; 4];

% size of values
[n,~] = size(val);

% Execution 1
for N = 1:1:n
    path = sprintf('%s%s%s', model,sub,block{N,1});
    set_param(path,'Value', num2str(val(N,1)));
end

Instead of the Execution 1, the following code also is applicable.
%Execution 2
for N = 1:1:n
    path = sprintf('%s%s%s', model,sub,block{N,1});
 
   % Catch errors.
   try
      set_param(path,'Value', num2str(val(N,1)));
   catch
       % If there is an error while doing this command,
       % the path where an error has occured is displayed on the command window.
     disp(path);
   end
end








No comments:

Post a Comment