Comparison of two BUS signals, while replacing signals that exist in the second BUS into the first BUS, BUS handling with Matlab Functions
    8 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
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 Kommentare
  Sebastian Gross
    
 am 25 Aug. 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, ...). 
You could have a look at this: https://www.mathworks.com/help/simulink/ug/approach-modeling-programmatically.html
Would this be helpful?
Best
Sebastian
Antworten (1)
  Altaïr
 am 17 Mär. 2025
        
      Bearbeitet: Altaïr
 am 17 Mär. 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'))
0 Kommentare
Siehe auch
Kategorien
				Mehr zu Sources finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


