Clear Filters
Clear Filters

Can you create an object in Simulink that can be referenced by multiple MATLAB Fcn blocks?

2 views (last 30 days)
I've attached an image with my current Simulink set-up. I am needing to replace the Interpreted MATLAB Fcn blocks with MATLAB Fcn blocks in order to make my Simulink code generation compatible. However, I am struggling to find a way to mimick this similar OOP approach - where I am essentially creating a global object that is being referenced and manipulated by multiple blocks.
With the MATLAB Fcn block, it is only creating a local object for that specific block. It cannot be referenced and manipulated by another MATLAB Fcn block. I run into a similar issue when trying it with MATLAB System blocks. Is there a workaround for this to get this "global object" character?
  5 Comments
Roop
Roop on 7 Aug 2024
Edited: Roop on 7 Aug 2024
I don't belive this method works for objects of user-defined classes. For instance, my object has properties that return variable size structs, App Designer UI's, and other complicated data types. Are there any addtional potential workarouds?
Umar
Umar on 8 Aug 2024
Hi @Roop,
To address your query regarding, “ I don't belive this method works for objects of user-defined classes. For instance, my object has properties that return variable size structs, App Designer UI's, and other complicated data types. Are there any addtional potential workarouds?”
I do understand that the set_param and evalin approach may not directly work for objects of user-defined classes with complex data types unless you utilize global variables within MATLAB because by declaring a variable as global within a function block, you can maintain its value across multiple function calls. Hope this helps.

Sign in to comment.

Answers (1)

Paul
Paul on 11 Jul 2024
Hi Roop,
If you want to use global data inside Matlab Function blocks, start from Use Global Data in MATLAB Function Blocks to assess if any of the provided methods are sufficient for your use-case.
I do not know if the Matlab Function blocks support use of user-defined classes (UDC) as seems to be the case here. Even if so, I'm not sure if a UDC instance can be used as a global object, and, frankly, I suspect that won't pe possible because this Simulink paradigm is based on Simulink.Signal objects. If it isn't there may be other workarounds.
  4 Comments
Roop
Roop on 8 Aug 2024
Hmm... yeah I think I might need to extract the particular data from my object that I am working with in Simulation. Unfortunately, my UDC is difficult to describe because proprietary work, but essentially I am intending to
  • Grab the object's current paramter data
  • Feed it into a S-Function (with MEX file) which is tied to an external model
  • Set the object's parameter data to the results
  • And continually iterate this process during the entire simulation time
Issue is maintaing the same instance of the object to read and write data to, which I was able to do with Interpreted Fcn blocks, but now that I need code generation compatibility, it is proving difficult to replace with MATLAB Fcn or MATLAB System blocks and get that same "global object" behavior.
Paul
Paul on 8 Aug 2024
Is there really a need to update the object during simulation?
Or do you just need to iterate on the data during simulation?

Sign in to comment.

Categories

Find more on Simulink Functions 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!