使用GUIDE的Matlab GUI:要动态更新图形 [英] Matlab GUI using GUIDE : Want to dynamically update graphs

查看:137
本文介绍了使用GUIDE的Matlab GUI:要动态更新图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've written a Matlab script that reads in data using a virtual COMM port in real-time. I've done a significant amount of signal processing in an mfile.

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

Next, I felt the need to have a compact GUI that displays the information as summary.

我直到最近才开始挖掘和阅读Matlab的内置GUI工具GUIDE的更多内容.我遵循了一些教程,并且能够在按下按钮后成功地将图形显示在GUI上.

I only recently started digging and reading more of Matlab's built-in GUI tool, GUIDE. I've followed a few tutorials and am successfully able to get my graphs to display on my GUI after a button-press.

但是,我希望 GUI实时更新.我的数据向量正在不断更新(从COMM端口读取数据).我希望GUI继续使用较新的数据更新图形,而不是依靠按下按钮进行更新.有人可以指出正确的方向进行后台更新吗?

However, I want the GUI to update in real-time. My data vector is constantly updating (reading in data from the COMM port). I want the GUI to keep updating the graphs with the newer data, as opposed to relying on a button press for an update. Can someone please point me in the right direction for background updating?

这是当前用于GUI的相关代码:

Here is the relevant code currently for the 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);

编辑更改的代码:

% --- 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);

我得到的错误是:

??? 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

推荐答案

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

Here is an example using a timer with a timerFcn callback. I made a simple GUI with 1 axes and 1 button.

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

In the opening function I initialize the plot and create the timer. In the start button callback I start the timer and start manipulating the data. The timer function callback the just updates the y-data of the line via its handle. Below are the relevant functions from the GUI's M-file (snipped init section and output 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);

您可能希望使用停止"按钮来停止计时器,具体取决于您的GUI的结构以及数据的更新方式.

You may want a Stop button to stop the timer depending on how your GUI is structured and were/how the data is updated.

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

Basic handles info some of this is pretty basic and you may already know it:

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

An individual handle to an object contains a bunch of properties that you can read with the get() function or set with the set() function. So for example maybe I wanted to change the text of the startButton for some reason in my GUI.

set(handles.startButton,'String','Something Other Than Start');

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

You may just want to set a break point in your code somewhere (maybe in a button press) and play around with the handles struct. Running get() commands on various objects to learn their properties.

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

Now the handles structure contains all of the ... umm... handles to your GUI's objects as well as any custom items that may be convenient for your to store there. Most GUI callbacks automatically get passed the handles struct so you have easy access to all parts of the GUI.

例如'startButton'回调自动传递给handles.因此,我可以轻松地通过handles.timer访问计时器对象.

Ex. The 'startButton' callback was automatically passed handles. So I had easy access to the timer object via handles.timer.

这使我将自定义内容粘贴到handles中.在打开函数中,我向句柄结构handles.timerhandles.plot添加了一个新项目,因为我知道它们在其他回调(例如按钮按下和timerFcn回调)中会很有用.

Which brings me to sticking custom things into handles. In the opening function I added a new item to the handles structure handles.timer and handles.plot because I knew they would be useful in other callbacks (like button press and the timerFcn callback).

但是,要永久存储这些内容,您需要使用'guidata'函数.该函数基本上要么存储修改后的handles结构,要么根据您的调用方式检索handles的副本.因此,打开功能的下一行是将修改后的handle结构(添加的.timer和.plot)存储到主GUI中.

However, to store these things permanently you need to use the 'guidata' function. This function basically either stores the modified handles struct or retrieves a copy of handles depending on how you call it. So the following line in the opening function is storing the modified handles structure (added .timer and .plot) into the main GUI.

guidata(hObject,handles);

基本上,每次在handles中添加内容时,都应具有该行以使更改永久生效.

Basically any time you add something in handles you should have that line to make the change permanent.

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

Now the other method of calling it is:

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

这将检索GUI的句柄结构.

This will retrieve the handles structure for the GUI.

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

And last handles.output = hObject is just the default output when you launch your GUI. IF you call your GUI via Matlab's command line like this h = myGUI; it should return the handle to your GUI.

这篇关于使用GUIDE的Matlab GUI:要动态更新图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆