How to work with Matlab object's within Simulink?

3 views (last 30 days)
How to work with the object in the base workspace in MATLAB Function in Simulink?
I want the object 'NewDriver' to perform some actions based on: Objects in the Workspace and input from Simulink simulation.
function steer_angle = Driver(newposition)
global NewPath;
global NewVehicle;
global NewDriver;
NewVehicle.Set_Position(newposition); %Updates the Vehicle Position in Space
steer_input = NewDriver.Run(NewVehicle, NewPath); %Calculates the Steering Input to keep the Vehicle on the Path
NewVehicle.Set_Steering_Position(steer_input); %Updates actulal Steering Position
steer_angle = steer_input; %Sends the Steering Angle to the simulation
I found the information to make this implementation with 'global' variables work their type has to be set in Model Explorer to Data Store Memory, but it generates an error:
Variable 'NewDriver' is resolved in workspace ('base') for block 'three_F1toPass_Driver/MATLAB Function2' but it is not a 'Simulink.Signal' object
Adding Simulink Signal with the same name (NewDriver) to Model Workspace doesn't work either.
I doubt if this is the best option to implement this functionality, I am totally open to any other ideas.
Is there a better option to perform this action in Simulink? By using different Simulink Blocks? All the functionalities I need were written in classes as methods to keep the code clean and understandable. However, now I am struggling with merging this functionality with Simulink Simulation.

Answers (1)

jessupj
jessupj on 13 Jan 2022
Edited: jessupj on 13 Jan 2022
see documtation for assignin to pass info from matlab to simulink perhaps?
i don't know if this is helpful or something you've looked into or fits your intended workflow.
modelworkspace = get_param(systemname,'ModelWorkspace'); %system name is the simulink system
assignin(modelworkspace,'simulink_variable_name',simulink_variable_value);
to assign matlab workspace variable simulink_variable_value to the 2nd argument

Categories

Find more on General Applications in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!