Matlab 实时绘制同一图形

Matlab 实时绘制同一图形,matlab,simulink,Matlab,Simulink,所以,我是Simulink的新手,具备基本的Matlab技能。我正试图用投影仪(作为第二台显示器连接)在地板上绘制障碍物。 不管怎样,我想一直在同一个数字上绘图,但我遇到了问题。有时图形在启动模拟时打开,有时不打开。我不能为我的生活,找出原因 绘图的基本思想是在地板上绘制障碍物,障碍物以与跑步机相同的速度向用户移动,从而使跑步机感觉它实际上在地板上。我已经删除了图中的所有元素,只显示了一个红色条作为障碍物和一个黑色背景 function plot_fcn(x) persistent f

所以,我是Simulink的新手,具备基本的Matlab技能。我正试图用投影仪(作为第二台显示器连接)在地板上绘制障碍物。 不管怎样,我想一直在同一个数字上绘图,但我遇到了问题。有时图形在启动模拟时打开,有时不打开。我不能为我的生活,找出原因

绘图的基本思想是在地板上绘制障碍物,障碍物以与跑步机相同的速度向用户移动,从而使跑步机感觉它实际上在地板上。我已经删除了图中的所有元素,只显示了一个红色条作为障碍物和一个黑色背景

function plot_fcn(x)

persistent f 

    projectionArea=3; %3m - arbitrary, will change later
    barLength=0.35; %0.35m - arbitrary, will change later
    no_contact=true; % contact indicator
    treadmillSpeed=10/9; %4km/h = 10/9 m/s
    refreshRate= 100; % 100Hz
    obstacleIncrement=treadmillSpeed/refreshRate; % eye noticeable increment


if isempty(f)
        target=1; 
        beforeBar=1;
        p=[10;901;1680;1027.5];
        f = figure;
        set(f, 'MenuBar', 'none', 'ToolBar', 'none', 'Color','black');
        set(0, 'DefaultFigurePosition', p);
        % target=x;
        while (no_contact) 

            afterBar=projectionArea-barLength-beforeBar;

            Y = [beforeBar, barLength, afterBar;
                 beforeBar, barLength, afterBar;
                 beforeBar, barLength, afterBar];

            f=area(Y);

            set(f,'EdgeColor','red');
            f(1).FaceColor = [0 0 0];
            f(2).FaceColor = [1 0 0];
            f(3).FaceColor = [0 0 0];

            if beforeBar>=projectionArea-(target+barLength/2)
                no_contact=false
            else 
                beforeBar=beforeBar+treadmillSpeed*obstacleIncrement;
                pause(obstacleIncrement)
            end
        end

        end
end

第二次调用该函数时未执行模拟,在上一次调用中打开的持久性图形保持打开状态。简而言之,
if isempty(f)
包含太多的代码。而
drawnow
有助于在窗口中迭代更新绘图

function plot_fcn()

persistent f

projectionArea=3; %3m - arbitrary, will change later
barLength=0.35; %0.35m - arbitrary, will change later
no_contact=true; % contact indicator
treadmillSpeed=10/9; %4km/h = 10/9 m/s
refreshRate= 100; % 100Hz
obstacleIncrement=treadmillSpeed/refreshRate; % eye noticeable increment

target=1;
beforeBar=1;
if isempty(f) || ~ishandle(f) || ~isa(f,'matlab.ui.Figure')
    f = figure;
    set(f, 'MenuBar', 'none', 'ToolBar', 'none', 'Color','black');
else
    delete(findobj(f,'type','Area'))
end
figure(f); %focus window
while (no_contact)
    afterBar=projectionArea-barLength-beforeBar;

    Y = [beforeBar, barLength, afterBar;
        beforeBar, barLength, afterBar;
        beforeBar, barLength, afterBar];

    aH=area(Y);

    set(aH,'EdgeColor','red');
    aH(1).FaceColor = [0 0 0];
    aH(2).FaceColor = [1 0 0];
    aH(3).FaceColor = [0 0 0];

    if beforeBar>=projectionArea-(target+barLength/2)
        no_contact=false;
    else
        beforeBar=beforeBar+treadmillSpeed*obstacleIncrement;
        pause(obstacleIncrement)
    end

    if isempty(f) || ~ishandle(f) || ~isa(f,'matlab.ui.Figure')
        f = figure;
        set(f, 'MenuBar', 'none', 'ToolBar', 'none', 'Color','black');
    else
        figure(f); %focus window
        drawnow
    end

end

第二次调用该函数时未执行模拟,在上一次调用中打开的持久性图形保持打开状态。简而言之,
if isempty(f)
包含太多的代码。而
drawnow
有助于在窗口中迭代更新绘图

function plot_fcn()

persistent f

projectionArea=3; %3m - arbitrary, will change later
barLength=0.35; %0.35m - arbitrary, will change later
no_contact=true; % contact indicator
treadmillSpeed=10/9; %4km/h = 10/9 m/s
refreshRate= 100; % 100Hz
obstacleIncrement=treadmillSpeed/refreshRate; % eye noticeable increment

target=1;
beforeBar=1;
if isempty(f) || ~ishandle(f) || ~isa(f,'matlab.ui.Figure')
    f = figure;
    set(f, 'MenuBar', 'none', 'ToolBar', 'none', 'Color','black');
else
    delete(findobj(f,'type','Area'))
end
figure(f); %focus window
while (no_contact)
    afterBar=projectionArea-barLength-beforeBar;

    Y = [beforeBar, barLength, afterBar;
        beforeBar, barLength, afterBar;
        beforeBar, barLength, afterBar];

    aH=area(Y);

    set(aH,'EdgeColor','red');
    aH(1).FaceColor = [0 0 0];
    aH(2).FaceColor = [1 0 0];
    aH(3).FaceColor = [0 0 0];

    if beforeBar>=projectionArea-(target+barLength/2)
        no_contact=false;
    else
        beforeBar=beforeBar+treadmillSpeed*obstacleIncrement;
        pause(obstacleIncrement)
    end

    if isempty(f) || ~ishandle(f) || ~isa(f,'matlab.ui.Figure')
        f = figure;
        set(f, 'MenuBar', 'none', 'ToolBar', 'none', 'Color','black');
    else
        figure(f); %focus window
        drawnow
    end

end

我还要补充一点,在while循环中重新使用
f
存储区域对象可能会导致意外行为,这不是一个好主意。例如,第一次运行
f
被覆盖为区域对象。关闭图,然后重新运行代码<代码>f不是空的,也不是预期的图形句柄。。。不会创建具有给定属性的新图形等@AeroEngy您是对的。以前我尝试过使用
isgraphics()
,但没有成功。我用
try catch
更新了代码。这不是一个干净的解决方案,但它是有效的。正如您所建议的,
persistent f
如果由于某些未知原因不需要,则应该删除。也许
ishandle(f)和&isa(f,'matlab.ui.Figure')
。。。这将确保它是一个有效的句柄(不是死的或删除的)&它是一个数字。而且,如果它是我的代码,那么“f=area(Y)”将变成
aH=area(Y)
,所以你不必担心
f
中包含的内容。是的,现在它更好了。谢谢大家。你帮了大忙。我还是新手,所以这是一条崎岖的道路:)我还要补充一点,在while循环中重新使用
f
存储区域对象可能会导致意外行为,这不是一个好主意。例如,第一次运行
f
被覆盖为区域对象。关闭图,然后重新运行代码<代码>f不是空的,也不是预期的图形句柄。。。不会创建具有给定属性的新图形等@AeroEngy您是对的。以前我尝试过使用
isgraphics()
,但没有成功。我用
try catch
更新了代码。这不是一个干净的解决方案,但它是有效的。正如您所建议的,
persistent f
如果由于某些未知原因不需要,则应该删除。也许
ishandle(f)和&isa(f,'matlab.ui.Figure')
。。。这将确保它是一个有效的句柄(不是死的或删除的)&它是一个数字。而且,如果它是我的代码,那么“f=area(Y)”将变成
aH=area(Y)
,所以你不必担心
f
中包含的内容。是的,现在它更好了。谢谢大家。你帮了大忙。我在这方面还是新手,所以这是一条崎岖不平的道路:)