How can I keep track of Handle Graphic objects in my MATLAB S-Function Block?
2 views (last 30 days)
Show older comments
MathWorks Support Team
on 6 Jul 2016
Edited: MathWorks Support Team
on 4 Mar 2021
I have a custom MATLAB S-Function block that is used to plot data while the simulation is running. How can I keep track of the handle graphic objects that are created while this block executes?
Accepted Answer
MathWorks Support Team
on 4 Mar 2021
Edited: MathWorks Support Team
on 4 Mar 2021
There are two main options that can be used to keep track of handle graphic objects:
1. Use tags and findobj.
This method works well if you will only ever need a single instance of the block in the model. If you may need multiple instances of the block in the model it may not work well as the value of the tag property is generally hard coded.
a. When the handle graphic object is created set the 'tag' property to a unique ID such 'myLine'
b. In later block methods when the object needs to be used use findobj in order to get a handle to the object>> h = findobj(0,'tag','myLine')
2. Use block UserData
This method works well if you may need multiple instances of the block in the model. This is because the block UserData is block instance specific, meaning that each instance of the block could easily have a reference to their own handle graphic object.
For a fully worked out example of this workflow please see the attached Simulink model and S-Function code.
a. When the handle graphic object is created store a copy of the object, or a structure that contains the object along with other data, in the block UserData>> ud = struct('h',figure)>> set_param(block,'UserData',ud);b. In later block methods when the object needs to be used get_param to get the object >> ud = get_param(block,'UserData');
Note that prior to R2014b MATLAB treated handles of handle graphic objects as variables of the double data type instead of objects. As a result the handle to the objects could also be stored in the Dwork vector of the S-Function. For more information on using Dwork vectors please see:
0 Comments
More Answers (0)
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!