Comparison of two BUS signals, while replacing signals that exist in the second BUS into the first BUS, BUS handling with Matlab Functions

6 vues (au cours des 30 derniers jours)
Hello,
i enconter a problem, while trying to implement BUS handling with Matlab Functions.
In my minimal example the Matlab Function i created to handle the case is working fine.
But i had to create the BUS for the output manually in the BUS editor.
The Problem is the BUS in my real problem is hughe and i am looking for a way to extract the BUS to load it into the Workspace.
I have not found a way that works. Has anybody encontered a Similar issue and found a solution?
I am using Matlab 2017b.
The Function looks like this:
function outbus = fcn(bus_original, bus_subsystem)
%#codegen
% Initialize the output bus by updating it with the original and subsystem buses
outbus = updateBus(bus_original, bus_subsystem);
end
function outbus = updateBus(bus_original, bus_subsystem)
% Get the field names of the input buses
original_names = fieldnames(bus_original);
subsystem_names = fieldnames(bus_subsystem);
% Initialize the output bus with the original bus
outbus = bus_original;
% Define tolerance for numerical stability
tol = 1e-6;
for i = 1:numel(original_names)
% Check if the current original signal name exists in the subsystem bus
match = strcmp(original_names{i}, subsystem_names);
if any(match) % If there's a match
if isstruct(bus_original.(original_names{i})) && isstruct(bus_subsystem.(subsystem_names{match}))
% If both matched fields are structures, update them recursively
outbus.(original_names{i}) = updateBus(bus_original.(original_names{i}), bus_subsystem.(subsystem_names{match}));
else
% Check for numerical stability
if abs(bus_original.(original_names{i}) - bus_subsystem.(subsystem_names{match})) > tol
% Update the signal value in the output bus
outbus.(original_names{i}) = bus_subsystem.(subsystem_names{match});
end
end
end
end
end
I tried loading the BUS into the Workspace when the MatlabFunction is not implemented with a ToWorkspace and ToFile Block.
I also tried the signal logging which didn`t work.
In the Example like i said i created it manually. But this is not a option for the big model.
Any help would be apreciated.
  2 commentaires
Sebastian Gross
Sebastian Gross le 25 Août 2023
Hi Ramon,
if I understand correctly, you want to use MATLAB to read and modify the Simulink Model (add a BusSelector, read parameters from the BusCreator, Add Parameters to the BusSelector, ...).
Would this be helpful?
Best
Sebastian
Ramón Tamino Uhl
Ramón Tamino Uhl le 27 Sep 2023
Hello Sebastián, I‘m sorry for the late response i was on vacation. At the end of the day what i tried to do with the function block is to do a rerouting. I have the original Signal called original_BUS going in. Then the Subsystem_BUS Signal and one Output_BUS. The Signals of the Subsystem_BUS should be replaced with the Signals in the Original_BUS when there is one and keeping the same structure and the Problem is the names of the sígnals don‘t match so i want to extract it from a Excel file where the Translation of the signals is documented. But using the Matlab Function Block i have trouble to pass the Bus Structure to the Matlab Function block and implementing this functionality of mapping the signals. Do you have some idea?

Connectez-vous pour commenter.

Réponses (1)

Altaïr
Altaïr le 17 Mar 2025
Modifié(e) : Altaïr le 17 Mar 2025
To address the task of manually creating a bus, a MATLAB script can be employed to read element names from an Excel file, facilitating the creation and storage of the bus for future use. Below is an example script for creating a simple bus with a flat hierarchy:
% Read the Excel file as a structure
mappingTable = readtable('busMappingExcel.xlsx');
mappingStruct = table2struct(mappingTable);
% Extract element names
orgBusElements = {mappingStruct.OrgBusElements};
subSysBusElements = {mappingStruct.SubSysBusElements};
% Create BusElement objects for orgBus
orgBusElementsArr = [];
for i = 1:numel(orgBusElements)
if isempty(orgBusElements{i})
continue;
end
busElem = Simulink.BusElement;
busElem.Name = orgBusElements{i};
orgBusElementsArr = [orgBusElementsArr, busElem];
end
% Create BusElement objects for subSysBus
subSysBusElementsArr = [];
for i = 1:numel(subSysBusElements)
if isempty(subSysBusElements{i})
continue;
end
busElem = Simulink.BusElement;
busElem.Name = subSysBusElements{i};
subSysBusElementsArr = [subSysBusElementsArr, busElem];
end
% Create Bus objects
orgBus = Simulink.Bus;
orgBus.Elements = orgBusElementsArr;
subSysBus = Simulink.Bus;
subSysBus.Elements = subSysBusElementsArr;
The busMappingExcel.xlsx file has the following content.
The script reads an Excel file into a structure (mappingStruct) to extract bus element names (OrgBusElements and SubSysBusElements). It iterates over these names to create Simulink.BusElement objects, storing them in arrays (orgBusElementsArr and subSysBusElementsArr). Two Simulink.Bus objects (orgBus and subSysBus) are created, with their Elements property set to the respective arrays of bus elements. This setup allows for the creation of buses with specified elements based on the Excel data. The busses created can be stored in a data dictionary for later use using the Simulink.data.dictionary.create function. The doc page can be accessed using the following command:
web(fullfile(docroot, 'simulink/slref/simulink.data.dictionary.create.html'))
For assigning values to bus elements with mapped names, consider using two additional buses with flat hierarchy as inputs to represent the mapping between element names. Additionally, exploring the Bus Assignment block might be advantageous. The documentation page can be accessed with this command:
web(fullfile(docroot, 'simulink/slref/busassignment.html'))

Community Treasure Hunt

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

Start Hunting!

Translated by