实时绘制相同的数字

问题描述 投票:0回答:1

所以,我是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
matlab simulink
1个回答
2
投票

第二次调用函数时未执行模拟,并且前一次调用中打开的持久图形保持打开状态。简而言之,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
© www.soinside.com 2019 - 2024. All rights reserved.