matlab function call class in workspace

7 views (last 30 days)
Haonan Xu
Haonan Xu on 11 Nov 2020
Edited: Swastik Sarkar on 23 Jun 2025
Hello,
First, I define a classdef named "ModelClass" with various properties, methods and functions.
Then I set global A = ModelClass and assign various values to properties of A.
A is now a global "1x1 ModelClass" value and saved in matlab workspace.
Now I'm trying to use A in Matlab Function in Simulink. Is it possible?
If yes, how can I do this.
Btw, I also have to generate S function of this Matlab Function. Therefore, I cannot use Interpreted Matlab Function.

Answers (1)

Swastik Sarkar
Swastik Sarkar on 23 Jun 2025
Edited: Swastik Sarkar on 23 Jun 2025
In MATLAB Level-2 S-functions, the recommended method for maintaining persistent data across simulation steps is through the use of DWork vectors. These vectors are designed to store numeric or logical values. However, it is important to note that MATLAB class objects are not supported within DWork vectors. This limitation is documented in the official MathWorks Documentation:
As an alternative, the UserData property of a Simulink block may be used to store an instance of a MATLAB class. This property allows arbitrary MATLAB data, including class objects, to be associated with a block.
An example demonstrating this technique is available on MATLAB Central:
Further information on the UserData property can be found in the MATLAB documentation:

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!