MATLAB: How to access the bus signals created with bus creater inside the matlab function block and pass bus as output from matlab function block

access bus in matlab fuctionbusmatlab function

Suppose I have created the bus of 5 signals with the help of bus creater block given the bus as input to matlab function block. I want to access individual signal from bus inside the matlab function block to do some manipulation with the signals and again pass the manipulated signal collectively as bus from matlab function block as a output.

Best Answer

See the attached model. MATLAB Function block takes a bus object input and outputs a bus object output. The ingoing signals are multiplied by 2 and outputted. Please see inside of MATLAB Function and File->Model Properties->Model Properties->Callbacks->PreLoadFcn for what I did exactly. The basic thing I did is to form a Simulink.Bus element and construct its elements by Simulink.BusElement. You will see these in PreLoadFcn. Then, inside the MATLAB Function, the key is to form a struct and assign to the output of the function. Also, if you go to Model Explorer->bus_creator_matlab_func->busInOut, you will see that it is necessary to define output y's DataType as Bus. Hope this helps.