How do I use a Bus in conjunction with a matlab function block in Simulink?

11 visualizaciones (últimos 30 días)
Filippo
Filippo el 7 de Mayo de 2025
Editada: Filippo el 12 de Mayo de 2025
I have a simulink file where i have a "Matlab Function Block" that needs to get a bus of parameters in input. My problem is that even after changing the variable type of the input to "Bus" and also changing the object name to the one i have given to the bus of parameters in question i still get the following error:
Expression 'Bus1' for type of data "Parameters" did not evaluate to a valid type.
Caused by: invalid setting in 'MATLAB Function' for parameter 'Datatype'
Unrecognised function or variable 'Bus1'. Variable 'Bus1' does not exist
Any idea of how i can solve he problem without using a selector before the function?
Thanks in advance

Respuestas (1)

Hitesh
Hitesh el 9 de Mayo de 2025
Hi Filippo,
You may be encountering this error due to the following reasons:
  • The MATLAB Function block expects the Bus object (named Bus1) to be defined in the base workspace or be accessible when the model is loaded/compiled.
  • When the data type assigned to one of the elements of the Bus is not a supported data type in Simulink.
You can address this error by following the below steps
Define the Bus Object in the Base Workspace
You need to make sure that the Bus object (Bus1) exists in the base workspace before you open or simulate the Simulink model.
  • Use a MATLAB Script : Create a script (e.g., create_my_buses.m) with the Bus definition.Run this script in MATLAB before opening or simulating your model:
% Create bus elements
elems(1) = Simulink.BusElement;
elems(1).Name = 'Param1';
elems(2) = Simulink.BusElement;
elems(2).Name = 'Param2';
% Create the bus object
MyBus = Simulink.Bus;
MyBus.Elements = elems;
Model Callbacks
  • Go to Model Properties > Callbacks > PreLoadFcn or InitFcn.
  • Paste your Bus object creation code there.
  • This will create the Bus object each time the model loads.
Data Dictionary (Recommended for larger projects)
  • Use a Simulink Data Dictionary to store your Bus objects.
  • Link your model to the data dictionary.
  • This avoids polluting the base workspace.
Check the Bus Object Name
  • Make sure the Datatype field in the MATLAB Function block's input exactly matches the Bus object name (Bus1).
  • The name is case-sensitive and variable name does not conflict with same variable name of different type.
For more information regarding "Using Bus in MATLAB function Block",kindly refer to following resources:
If your issue is still exists, kindly share the sample model which will reproduce this error.

Categorías

Más información sobre Composite Interfaces en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by