What I want to do is done in many blocks on simulink, for example the block LMS block, see below. Thanks!
How to include variable number of outputs on MATLAB System block in Simulink?
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Benjamin Campbell
el 26 de En. de 2023
Comentada: Benjamin Campbell
el 29 de Mzo. de 2023
I have a MATLAB System block in Simulink and want to have different number of outputs depending on checkboxes inside the system block. So, I have followed these instructions ( https://ch.mathworks.com/help/simulink/ug/system-block-customizing-dialog.html ) and the checkboxes assign true or false variables (about whether a certain output should be output or not). But I can not get it to work with different numbers of outputs.
If you download this code, and open simulink and put in Matlab system and select it you get the block. But it can't change the number of outputs and gives an error when you open and close the block with different boxes ticked.
Thank you so much in advance! Let me know if there is anything else needed.
classdef test_varying_outputs < matlab.System
properties (Nontunable)
% Output1 Do you want to output another scalar?
Output1 (1,1) logical = false;
% Output2 Do you want to output a 2D vector?
Output2 (1,1) logical = false;
% Output3 Do you want to output a 3D vector?
Output3 (1,1) logical = false;
end
methods (Access = protected)
% Initialise states
function setupImpl(~)
end
% Set up output data
function data_type_out = getOutputDataTypeImpl(obj)
% Get outputs data type.
data_type_out = cell(1, getNumOutputs(obj));
for i = 1:getNumOutputs(obj)
data_type_out{i} = 'double';
end
end
function output_complex = isOutputComplexImpl(obj)
% Is output complex
output_complex = cell(1, getNumOutputs(obj));
for i = 1:getNumOutputs(obj)
output_complex{i} = false;
end
end
function size_out = getOutputSizeImpl(obj)
% Return sizes of outputs
size_out = cell(1, getNumOutputs(obj));
size_out{1} = [1 1];
out_ind = 2;
if obj.Output1
size_out{out_ind} = [1 1];
out_ind = out_ind + 1;
end
if obj.Output2
size_out{out_ind} = [2 1];
out_ind = out_ind + 1;
end
if obj.Output3
size_out{out_ind} = [3 1];
end
end
function varargout = isOutputFixedSizeImpl(obj)
% Get outputs fixed size.
varargout = cell(1, getNumOutputs(obj));
for i = 1:getNumOutputs(obj)
varargout{i} = true;
end
end
% Code that executes each step
function varargout = stepImpl(obj, ref, e)
varargout{1} = 4; % Output that is always on
out_ind = 2;
if obj.Output1
varargout{out_ind} = 2;
out_ind = out_ind + 1;
end
if obj.Output2
varargout{out_ind} = [4 5];
out_ind = out_ind + 1;
end
if obj.Output3
varargout{out_ind} = [3.2 3.3 3.4];
end
end
function resetImpl(~)
end
end
% Block icon and dialog customizations
methods (Static, Access = protected)
function header = getHeaderImpl
header = matlab.system.display.Header(...
'lmsSysObj', ...
'Title', 'different number of outputs');
end
function tabs = getPropertyGroupsImpl
group = matlab.system.display.SectionGroup(...
'Title', 'Outputs', ...
'propertyList',{'Output1','Output2','Output3'});
% Add a button that calls back into the visualize method
group.Actions = matlab.system.display.Action(@(actionData,obj)...
visualize(obj,actionData),'Label','Visualize');
tabs = group;
end
end
methods (Access = protected)
function icon = getIconImpl(~)
icon = sprintf('different number of outputs');
end
function [in1name, in2name] = getInputNamesImpl(~)
in1name = 'in1';
in2name = 'in2';
end
function varargin = getOutputNamesImpl(obj)
varargin{1} = 'out';
out_ind = 2;
if obj.Output1
varargin{out_ind} = 'out scalar';
out_ind = out_ind + 1;
end
if obj.Output2
varargin{out_ind} = "out 2x1";
out_ind = out_ind + 1;
end
if obj.Output3
varargin{out_ind} = 'out 3x1';
end
end
end
end
Respuesta aceptada
Balavignesh
el 15 de Mzo. de 2023
Hi Benjamin Campbell,
As per my understanding, you want to change the number of outputs in the MATLAB System block depending on the properties set.
As per the documentation of ”getNumOutputsImpl”, use “nargout” in “stepImpl” method when you have variable number of outputs. Please refer to the example below.
methods (Access = protected)
function varargout = stepImpl(~,varargin)
for i = 1:nargout
varargout{i} = varargin{i}+1;
end
end
end
I hope this resolves your queries.
Más respuestas (0)
Ver también
Categorías
Más información sobre Customize System Objects for Simulink en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!