【问题标题】:Showing data on Matlab GUI which is continuously being updated in a separate Matlab function在 Matlab GUI 上显示数据,这些数据在单独的 Matlab 函数中不断更新
【发布时间】:2015-01-23 09:31:52
【问题描述】:

我在 Matlab 中有一个从硬件获取连续传感器值的函数。当有新值可用时,它会给出一个标志,我们可以更新保存这些值的变量。下面是一个虚拟函数来模拟这个函数的作用。

function example( )
% Example function to describe functionality of NatNetOptiTrack

% Hardware initialization,    
% Retriving real time information continuously

for i = 1:100   %in real time this loop runs for ever
   data = rand(3,6);
   % Send the updated data to gui in each iteration
end

end

我用guide做了一个gui,如图:

所以要显示的数据是一个 3x6 矩阵,其中列对应于 X Y Z Roll Pitch 和 Yaw 值,而行对应于 Objects。

我想在 gui 上显示此函数的持续更新值。有没有办法我可以在我的示例函数中初始化 gui 并通过使用循环中的句柄来更新输出值。我尝试将示例函数中的 gui 代码复制为脚本,它能够初始化但无法识别句柄。 当我按下按钮时,我还想在命令窗口上显示当前值。

谢谢

【问题讨论】:

    标签: matlab user-interface


    【解决方案1】:

    如果您启动 GUI 然后运行该函数,您应该能够获取 GUI 上的控件的句柄,前提是您使 GUI 图形句柄可见并将其标记/名称设置为适当的值。在 GUIDE 中,打开 GUI 的 Property Inspector 并将 HandleVisibility 属性设置为 on,并将 Tag 属性设置为 MyGui(或其他名称)。然后在您的 example.m 文件中执行以下操作

    function example( )
        % Example function to describe functionality of NatNetOptiTrack
    
        % get the handle of the GUI
        hGui = findobj('Tag','MyGui');
    
        if ~isempty(hGui)
            % get the handles to the controls of the GUI
            handles = guidata(hGui);
        else
            handles = [];
        end
    
    
        % Hardware initialization,    
        % Retriving real time information continuously
    
        for i = 1:100   %in real time this loop runs for ever
           data = rand(3,6);
    
           % update the GUI controls
           if ~isempty(handles)
    
               % update the controls
               % set(handles.yaw,…);
               % etc.
           end
    
           % make sure that the GUI is refreshed with new content
           drawnow();
        end
    end
    

    另一种方法是将example 功能代码复制到您的 GUI 中 - 硬件初始化可能发生在您的 GUI 的 _OpeningFcn 中,您可以创建一个(周期性)计时器来与硬件通信并将数据获取到显示在 GUI 上。

    至于在按下 GUI 按钮时显示当前数据,您可以通过使用 fprintf 将 GUI 控件的内容写入命令行/窗口来轻松地做到这一点。不过,您需要使您的 example 函数可中断,以便按钮可以中断该连续运行的循环。您可以通过添加在循环的每次迭代结束时执行的pause 调用(一定毫秒数)来执行此操作,或者只使用上面的drawnow 调用(这就是为什么我将它放在 if 语句之外 - 这样它就会在循环的每次迭代中被调用。

    试试上面的方法,看看会发生什么!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      相关资源
      最近更新 更多