在单独的 Matlab 函数中不断更新的 Matlab GUI 上显示数据

Showing data on Matlab GUI which is continuously being updated in a separate Matlab function

我在 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 值,而行对应于对象。

我想在 gui 上显示这个函数不断更新的值。有没有一种方法可以在示例函数中初始化 gui 并使用循环中的句柄更新输出值。我尝试将示例函数中的 gui 代码复制为脚本,它能够初始化但无法识别句柄。 我还想在按下按钮时在命令 window 上显示当前值。

谢谢

如果您启动 GUI,然后 运行 函数,您应该能够获得 GUI 上控件的句柄,前提是您使 GUI 图句柄可见并设置其 tag/name 适当的东西。在 GUIDE 中,打开 GUI 的 属性 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按钮时显示当前数据,您可以通过将GUI控件的内容写入命令line/window和fprintf来轻松实现。不过,您需要使 example 功能可中断,以便按钮可以连续中断 运行ning 循环。您可以通过添加在循环的每次迭代结束时执行的 pause 调用(持续一定的毫秒数)来执行此操作,或者只使用上面的 drawnow 调用(这就是为什么我将它放在 if 语句之外的原因 - 这样它就会在循环的每次迭代中被调用。

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