YAKOVM
YAKOVM

Reputation: 10153

Simulink Mask initialization command and RuntimeObject

Here is code for my mask initialization I want to use it to change the color according to outputs of one of the blocks

systems = find_system(gcb,'LookUnderMasks' , 'on', 'FollowLinks','on', 'SearchDepth', 1,'regexp','on','Name','Multi');
rto=get_param(systems{1,1},'RuntimeObject')
if rto.OutputPort(1).Data == 1
    set_param(gcb,'BackgroundColor','red')
else
    set_param(gcb,'BackgroundColor','green')
end

When I press OK I get Error: Dot indexing is not supported for variables of this type

When I use keyboard to debug I get this

K>> rto

rto =

    handle

It seems that it can't get the runtimeobject but when I do this while still in debug mode

rto=get_param(systems{1,1},'RuntimeObject');

while debugging I did get it

rto =

    Simulink.RunTimeBlock

Upvotes: 0

Views: 2059

Answers (1)

Phil Goddard
Phil Goddard

Reputation: 10772

Your approach won't work. Mask initialization happens during model initialization, at which point the block output may not even have a value. Even if the code you show did execute, because mask initialization does not happen as the simulation is progressing, the color of your block would not change as the signal value changed.

The only way to do it (i.e. change the block color) is to have code that executes at every time step as the simulation progresses. This is typically achieved using an S-Function. Below is a very simple S-Function that will change the color of the Subsystem that it is in based on the value of the block's input signal.

enter image description here

The code in the S-Function is:

function msfcn_times_two(block)
% Level-2 MATLAB file S-Function for times two demo.
%   Copyright 1990-2009 The MathWorks, Inc.

setup(block);

%endfunction

function setup(block)

%% Register number of input and output ports
block.NumInputPorts  = 1;
block.NumOutputPorts = 1;

%% Setup functional port properties to dynamically
%% inherited.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;

%% Set block sample time to inherited
block.SampleTimes = [-1 0];

%% Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';

%% Run accelerator on TLC
block.SetAccelRunOnTLC(true);

%% Register methods
block.RegBlockMethod('Outputs',                 @Output);

%endfunction

function Output(block)

if block.InputPort(1).Data < -0.5
    set_param(get_param(block.BlockHandle,'Parent'),'BackgroundColor','red')
elseif block.InputPort(1).Data < 0.5
    set_param(get_param(block.BlockHandle,'Parent'),'BackgroundColor','green')
else
    set_param(get_param(block.BlockHandle,'Parent'),'BackgroundColor','blue')
end
%endfunction

Upvotes: 1

Related Questions