使用GUIDE的Matlab GUI:想要动态更新图形

c0d*_*3rz 8 matlab matlab-guide

我编写了一个Matlab脚本,可以使用虚拟COMM端口实时读取数据.我在mfile中完成了大量的信号处理.

接下来,我觉得需要一个紧凑的GUI,将信息显示为摘要.

我最近才开始挖掘和阅读更多Matlab的内置GUI工具GUIDE.我已经按照了一些教程,成功地能够在按下按钮后让我的图形显示在我的GUI上.

但是,我希望GUI能够实时更新.我的数据向量不断更新(从COMM端口读取数据).我希望GUI能够使用更新的数据更新图表,而不是依靠按钮进行更新.有人可以指出我正确的方向进行背景更新吗?

以下是目前GUI的相关代码:

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data
global time

% Time domain plot
axes(handles.timeDomainPlot);
cla;
plot (time, data);
Run Code Online (Sandbox Code Playgroud)

编辑更改代码:

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%Setting it to display something when it ends
% t = timer('TimerFcn', 'timerOn=false; disp(''Updating GUI!'')',... 
t = timer(... 
            'TasksToExecute', 10, ... % Number of times to run the timer object
            'Period', 3, ...                
            'TimerFcn', GUIUpdate()); 

%Starting the timer
start(t)

function GUIUpdate()
global data
global time
%Parameters below axes
    global min
    global max 
      % Time domain plot
    axes(handles.timeDomainPlot);
    cla;
    plot (time, data);
    %Other parameters:
    set(handles.mean, 'String', mean);
    set(handles.max, 'String', max);
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

??? Error using ==> GUI_Learning>GUIUpdate
Too many output arguments.

Error in ==>
@(hObject,eventdata)GUI_Learning('pushbutton1_Callback',hObject,eventdata,guidata(hObject))


??? Error while evaluating uicontrol Callback
Run Code Online (Sandbox Code Playgroud)

Aer*_*ngy 10

以下是使用带有timerFcn回调的计时器的示例.我用一个轴和一个按钮做了一个简单的GUI.

在打开功能中,我初始化绘图并创建计时器.在开始按钮回调中,我启动计时器并开始操作数据.定时器函数回调只是通过其句柄更新行的y数据.以下是GUI的M文件(剪切的init部分和输出fcn)中的相关函数.

function testTimer_OpeningFcn(hObject, eventdata, handles, varargin)
global y x
x = 0:.1:3*pi; % Make up some data and plot
y = sin(x);
handles.plot = plot(handles.axes1,x,y);
handles.timer = timer('ExecutionMode','fixedRate',...
                    'Period', 0.5,...
                    'TimerFcn', {@GUIUpdate,handles});
handles.output = hObject;
guidata(hObject, handles);

% --- Executes on button press in startButton.
function startButton_Callback(hObject, eventdata, handles)
global y x
start(handles.timer)
for i =1:30
   y = sin(x+i/10); 
   pause(1) 
end

function GUIUpdate(obj,event,handles)
global y 
set(handles.plot,'ydata',y);
Run Code Online (Sandbox Code Playgroud)

您可能需要一个"停止"按钮来停止计时器,具体取决于GUI的结构以及数据的更新方式.

编辑:基本处理信息其中一些是非常基本的,你可能已经知道它:

对象的单个句柄包含一组属性,您可以使用get()函数读取这些属性,或使用set()函数进行设置.因此,例如,我想在GUI中出于某种原因更改startButton的文本.

set(handles.startButton,'String','Something Other Than Start');
Run Code Online (Sandbox Code Playgroud)

您可能只想在代码中设置一个断点(可能是按下按钮)并使用句柄结构.get()在各种对象上运行命令以了解其属性.

现在,句柄结构包含GUI对象的所有... umm ...句柄以及可能方便您存储的任何自定义项目.大多数GUI回调会自动传递handle struct,因此您可以轻松访问GUI的所有部分.

防爆.'startButton'回调自动传递handles.所以我可以轻松访问计时器对象handles.timer.

这让我开始坚持定制的东西handles.在打开函数中,我向句柄结构添加了一个新项目handles.timer,handles.plot因为我知道它们在其他回调中很有用(比如按下按钮和timerFcn回调).

但是,要永久存储这些东西,您需要使用'guidata'功能.此函数基本上存储修改后的handles结构或handles根据您调用它的方式检索副本.因此,open函数中的以下行是将修改后的句柄结构(添加.timer和.plot)存储到主GUI中.

guidata(hObject,handles);
Run Code Online (Sandbox Code Playgroud)

基本上任何时候你添加的东西handles都应该有这条​​线来使变化永久化.

现在调用它的另一种方法是:

handles = guidata(hObject); %hObject can be any handle who is a child of the main GUI.
Run Code Online (Sandbox Code Playgroud)

这将检索GUI的句柄结构.

最后一个handles.output = hObject是启动GUI时的默认输出.如果您通过Matlab的命令行调用GUI,h = myGUI; 它应该返回GUI的句柄.