Azzera filtri
Azzera filtri

How to write a specific bus signal label to a string variable

4 visualizzazioni (ultimi 30 giorni)
Hello all,
this might be an odd question, but I want to know if there is a way to write the full path of a bus signal to a string variable. I need to be able to find the correct signal using either indexing or value comparison.
To start: my model uses busses and subbusses to organise data and near the top level of my model, a number of bus signals are compared and the maximum value is extracted from those signals. See image below
Some of these signals are in the top level of the bus, but most are in a deep subbus (i.e. Bus.subbus1.subbus2.subbus3.signal). I do have a way to check which of the selected bus signals was the maximum value (see image and Matlab function below). What I want, though, is to have Simulink tell me the full name of the signal that holds the maximum value.
function y = fcn(u)
Max_is_true = u; % Vector of Boolean values. The index of the first 'True' value is the index of the worst case scenario
Indices_of_max = find(Max_is_true,1); % Vector of indices where the value is non-zero. In this case, the indices with 'True' values
y = Indices_of_max(1); % use of indexing to prevent variable size matrix error
The bus selector selects 50 signals from the bus, but the bus contains a lot more than that. So I have a way to know which of the 50 selected signals holds the highest value, now I only need to know the full name of that signal.
For example: The signal with the highest value is the 35th signal out of the bus selector block. The 35th signal's full name is Bus.Subbus3.Subbus5.Subbus1.Signal. Is there a way to write the full name of the signal to a variable, and have that variable as an output in the top level of my model?
Extra difficulty factor: the model must be compatible with Matlab Code Generation. A CoDeSys script must be generated from the final Simulink model
Thanks in advance!

Risposte (1)

madhan ravi
madhan ravi il 13 Dic 2023
function y = findMaxSignal(u)
% Assuming 'u' is the input vector representing the signals
[~, maxIndex] = max(u);
% Assuming 'busName' is the name of the bus
busName = 'Bus'; % Replace with the actual name of your bus
% Assuming 'busObject' is the Simulink bus object
busObject = Simulink.Bus.createObject(busName);
% Extract full signal name using the index
fullSignalName = busObject.Elements(maxIndex).Name;
% Store the full signal name in a variable
assignin('base', 'maxSignalName', fullSignalName);
% Output the variable
y = fullSignalName;
end
I haven"t tested it using code generation.
  3 Commenti
madhan ravi
madhan ravi il 13 Dic 2023
Could you please show me the code you used? And attach the model if possible.
Dolf
Dolf il 13 Dic 2023
I've attached a simple representation of my model, because the complete model is quite large. The actual model should behave the same as this one, regarding busses.
The code within the model looks like this
function y = findMaxSignal(u)
% Assuming 'u' is the input vector representing the signals
[~, maxIndex] = max(u);
% Assuming 'busName' is the name of the bus
busName = 'TopBus'; % Replace with the actual name of your bus
% Assuming 'busObject' is the Simulink bus object
busObject = Simulink.Bus.createObject(busName);
% Extract full signal name using the index
fullSignalName = busObject.Elements(maxIndex).Name;
% Store the full signal name in a variable
assignin('base', 'maxSignalName', fullSignalName);
% Output the variable
y = fullSignalName;

Accedi per commentare.

Prodotti


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by