如何使功能无阻塞? Matlab GUI中的动态绘图 [英] How to make a function non-blocking? Dynamic plotting in Matlab GUI

查看:193
本文介绍了如何使功能无阻塞? Matlab GUI中的动态绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从使用Matlab以来已经有一段时间了,到目前为止,我从未将它用于任何GUI创建.我的目标是要有一个可以按下的按钮,然后在计算结果时绘制结果.该按钮应根据发生的情况在开始"和停止"之间切换.收集这些结果进行几次迭代,每次迭代都提供另一个数据点.

It's been some time since using Matlab and I've never used it for any GUI creation until now. My goal is to have a button which I can press, and then have the results plotted as they are calculated. The button should toggle between 'Start' and 'Stop' depending on what is happening. These results are collected for several iterations and each iteration gives another data point.

我的解决方案是将轴传递给函数进行计算,然后可以绘制到轴上.这是可行的,但是在这种情况下,直到绘制完成后,按钮才会切换到停止".我可以使该功能无阻塞吗?我什至会以最好的方法来解决这个问题?如何使用停止"按钮停止计算?我是否只需要为此创建一个线程(matlab是否支持线程)?

My solution was to pass the axes to the function doing the calculations, which can then plot to the axes. This works, however while this is happening the button won't toggle to 'Stop' until after the plotting is complete. Can I make the function non-blocking? Am I even going about this is in the best method possible? How will I be able to stop the calculations with the 'Stop' button? Will I just need to create a thread for this (does matlab support threading)?

我一直在用一个简单的函数绘制正弦来测试我的想法

I have been testing my ideas with a simple function to draw a sine

function [ t,y ] = slowSin(ax)
%Plot a sin curve slowly
t = [0:0.06:4*pi];
y = sin(1.5*t);

for i = 1:length(t)
    plot(ax, t(1:i), y(1:i))
    pause(0.1)

end

直到现在我还没有想到要穿线.我会尽快进行调查,但是我们会尽力帮助您.

I haven't thought of threading until now. I will look into that shortly, but all help is appreciated.

推荐答案

首先,MATLAB不对任何图形进行多线程处理,因此您必须发挥创造力.

First of all, MATLAB does not do multithreading for any graphics so you have to get creative.

此外,您还需要使用 drawnow 来如果要在计算过程中进行一些绘制,则刷新回调和渲染事件.

Also, you'll want to use drawnow to flush the callback and rendering events if you're trying to do some plotting in the middle of your computations.

就知道何时停止,您也许可以将按钮的图形手柄传递给您的计算,并且它可以在每次迭代时检查该值?

As far as knowing when to stop, you can maybe pass the graphics handle of the button to your computations, and it can check the value each iteration?

我有一个示例,该示例使用永久变量进行跟踪的当前迭代,并允许用户通过单击切换按钮来暂停"计算.

I have an example that uses a persistent variable to keep track of the current iteration and allows the user to "pause" the calculation by unclicking the toggle button.

function guiThing()

    figure();
    hbutton = uicontrol('style', 'toggle', 'String', 'Start');

    hplot = plot(NaN, NaN);

    nIterations = 1000;
    set(gca, 'xlim', [1 nIterations], 'ylim', [1 nIterations])

    set(hbutton, 'callback', @(s,e)buttonCallback())

    function buttonCallback()
        % If the button is depressed, calculate the thing
        if get(hbutton, 'value')
            calculateThing(hbutton, hplot, nIterations);
        end
    end
end

% This function can live anywhere on the path!
function calculateThing(hbutton, hplot, nIterations)

    % Keep track of data with persistent variables
    persistent iteration

    % First time to call this function, start at 1
    if isempty(iteration); iteration = 1; end

    for k = iteration:nIterations
        iteration = k;

        % If the button is not depressed then stop this
        if ~get(hbutton, 'value')
            return
        end

        % Update plotting in some way
        curdata = get(hplot);
        set(hplot, 'XData', [curdata.XData k], ...
                   'YData', [curdata.YData, k])

        % Flush drawing queue
        drawnow

        fprintf('Iteration: %d\n', iteration);
    end
end

您可以使用持久变量来跟踪需要在迭代之间(以及停止和开始)之间持久的任何其他数据.

You can use the persistent variables to keep track of any other data that needs to persist between iterations (and stops and starts).

这篇关于如何使功能无阻塞? Matlab GUI中的动态绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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