另一种方法是使用 setappdata 和 getappdata 从 GUI 中的任意位置获取数据。
例如,在importfile2.m 的末尾,您可以使用 setappdata 将数据存储在某个变量中。第一个参数告诉 MATLAB 在哪个工作区中保存它。例如,您可以使用 GUI 界面本身或使用可从任何地方访问的基本工作区。这是最通用的方式:
setappdata(0,'FancyName',YourData); %// The 0 is for the base workspace,i.e. the 'root'.
%//YourData is the actual data and 'FancyName' is whatever name you give them. It does not have to be the same name as the variable in your function. The important thing is to use the same name in getappdata as below.
如果您只想将数据与 GUI 图相关联,您可以使用以下内容:
setappdata(handles.YourFigure_Tag,'FancyName',YourData);
要在 GUI 中获取数据,请在其打开函数(或任何您想要的回调)中使用 getappdata,然后您就可以开始了:
Data_inGUI = getappdata(0,'FancyName'):
更稳健的方法是将数据直接存储在 GUI 的句柄结构中,以便可以从每个回调中访问:
handles.Data_inGUI = getappdata(0,'FancyName'):
guidata(hObject,handles); %// Update handles structure; important!
应该这样做。希望对您有所帮助!
EDIT 我认为另一种解决方案是在导入函数的末尾保存一个.mat 文件并将其加载到GUI 的OpeningFcn 中。比可能更简单/更快。
编辑 2 根据您在下面的评论,我会这样做:
1) 在GUI的OpeningFcn中,导入数据。
[Date,OutAirTemp,SupAirtemp] = importfile3('AHU7Oct.csv')
然后你就可以把所有东西都存储在handles结构中了:
handles.Data = Date;
handles.OutAirTemp = OutAirTemp;
handles.SupAirtemp = SupAirtemp;
guidata(hObject,handles); %// Update handles structure.
然后在 GUI 的其他地方(即其他回调),您可以正常获取数据,即使用例如:
NewDate = handles.Date - 4 %// or whatever.
是不是清楚一点?