开发者

What's the proper style for having multiple figures accessing the same data?

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 h开发者_Go百科andle 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 getappdata and setappdata to associate data with an individual figure:

%# Associate some data to the main figure handle...
setappdata(main_FH, 'myData', data);

%# Retrieve that data from the main figure handle
myData = getappdata(main_FH, 'myData');

%# check if some app data exists for main_FH
validAppData = isappdata(main_FH, 'myData');

You could also use set(FH, 'UserData', myData) (and get() too), although there's only one UserData property for every handle; you could set it to a struct and use isfield() rather than isappdata() to see if the field in myData exists.

Finally, there's guidata, but this is essentially a wrapper for ___appdata for GUIDE GUIs.

There's a summary of the ways to pass data like this on The MathWorks website.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜