I’m currently refactoring a bunch of old Matlab code (pre new-oop Matlab), and the GUI code is a mess.
The GUI is basically a bunch of separate Matlab figures, each of which must present the same data in a different way.
The old code handles this problem by using a global struct to hold all of data to be displayed along with the meta-data (mainly information about the size needed to graph the current data).
My question is whether or not this is the right way, stylistically, to do this in the current version of Matlab. I’ve considered bundling the data into a handle class and the meta-data into another, and then passing both to every figure in the GUI, but I don’t know if the added encapsulation is worth the messiness of the added arguments.
Are there any general style rules for making such decisions in Matlab GUI programming?
There are a couple ways to do this. You can use
getappdataandsetappdatato associate data with an individual figure:You could also use
set(FH, 'UserData', myData)(andget()too), although there’s only oneUserDataproperty for every handle; you could set it to a struct and useisfield()rather thanisappdata()to see if the field inmyDataexists.Finally, there’s
guidata, but this is essentially a wrapper for___appdatafor GUIDE GUIs.There’s a summary of the ways to pass data like this on The MathWorks website.