【问题标题】:how to update axes in guide - matlab如何更新指南中的轴 - matlab
【发布时间】:2016-11-17 18:21:56
【问题描述】:

我在 MATHLAB 中使用 GUI 和 Mfile 编写代码。在我的程序中,我有一个板子。每次,计算机放一个弹珠并旋转板子,然后从用户那里得到一个弹子位置并旋转板子的方向,在每件事之后,我希望它在 GUI 中的轴 1 中显示结果。但它在下面代码的第 9 行中不起作用:

% some codes...
1.   while(currentDepth < 7)
2.   if(mod(currentDepth,2) ~= (plrC-1))
3.       plat(currentDepth);
4.       drawTable(); % show the result in axes1 --> it works
5.   else
6.       getMarble();
7.       drawTable(); % show the result in axes1 --> it works
8.       rotate();
9.       drawTable(); % show the result in axes1 --> it dosen't work
10.  end
11.  end
% some codes...

.

function drawTable()
global board;
% some codes...
imshow(board,[0 4]);
end

你有什么想法吗?

它是旋转功能。板分为 4 个部分,只需旋转 1 个部分。

function rotate()
global board;
block = 0;
vector = 'non';
while(block<1 || block>4)
    block = str2double(cell2mat(inputdlg('chose a block: 1/2/3/4','board rotation')));
end
switch block
    case 1
        k=1; z=1;
    case 2
        k=1; z=4;
    case 3
        k=4; z=1;
    case 4
        k=4; z=4;
end
while(~strcmp(vector,'left') && ~strcmp(vector,'right'))
    vector = questdlg('rotate left or right','Rotation','left','right','right');
end
if(strcmp(vector,'left'))
    board(k:k+2,z:z+2)=rot90(board(k:k+2,z:z+2));
else
    board(k:k+2,z:z+2)=rot90(board(k:k+2,z:z+2),3);
end
end

好的,现在我们有一个简化的代码。使用轴创建一个新的 GUI,然后从“OpeningFnc”运行下面的代码。你会看到我的问题。

function test()
currentDepth = 1;
plrC = 1;
plrO = 2;
board = zeros(6);
while(currentDepth < 40)
    if(mod(currentDepth,2) == 1)
        plat();
        drawTable(); % show the result in axes1 --> it works
    else
        getMarble();
        drawTable(); % show the result in axes1 --> it works
        rotate();
        drawTable(); % show the result in axes1 --> it dosen't work
    end
    currentDepth = currentDepth +1;
end

function plat()
    for a=1:5000
       for b=1:5000
           for c=1:50
           m = a + b;
           end
       end
    end
    row = 1;
    column = 1;
    while(board(row,column) ~= 0)
        row = randi(6);
        column = randi(6); 
    end
    board(row,column) = plrC;
    row = randi([1 4]);
    column = randi([1 4]);
    board(row:row+2,column:column+2)=rot90(board(row:row+2,column:column+2));
end
function drawTable()

    board(board==0) = board(board==0)+4;
    B = zeros(305);
    B(:,152:154) = 3;
    B(152:154,:) = 3;
    for i=1:6
        for j=1:6
            x = (i*5)+1+(i-1)*45;
            y = (j*5)+1+(j-1)*45;
            B(x:x+44,y:y+44) = board(i,j);
        end
    end
    imshow(B,[0 4]);
    board(board==4) = board(board==4)*0;
end
function getMarble()

    board(board==0) = board(board==0)+4;
    b = zeros(305);
    b(:,152:154) = 3;
    b(152:154,:) = 3;
    for i=1:6
        for j=1:6
            x = (i*5)+1+(i-1)*45;
            y = (j*5)+1+(j-1)*45;
            b(x:x+44,y:y+44) = board(i,j);
        end
    end
    imshow(b,[0 4]);
    i = 0;
    while(i~=4)
        [x,y] = ginput(1);
        if(x<0 || x>305 || y<0 || y>305)
            i = 0;
        else
            i = b(ceil(y),ceil(x));
        end
    end
    y = ceil(y/50);
    x = ceil(x/50);
    board(y,x) = plrO;
end
function rotate()

    block = 0;
    vector = 'non';

    while(block<1 || block>4)
        block = str2double(cell2mat(inputdlg('chose a block: 1/2/3/4','board rotation')));
    end
    switch block
        case 1
            k=1; z=1;
        case 2
            k=1; z=4;
        case 3
            k=4; z=1;
        case 4
            k=4; z=4;
    end
    while(~strcmp(vector,'left') && ~strcmp(vector,'right'))
        vector = questdlg('rotate left or right','Rotation','left','right','right');
    end
    if(strcmp(vector,'left'))
        board(k:k+2,z:z+2)=rot90(board(k:k+2,z:z+2));
    else
        board(k:k+2,z:z+2)=rot90(board(k:k+2,z:z+2),3);
    end
end

结束

【问题讨论】:

  • 这是自定义旋转功能吗?如果有,请提供代码
  • “但它在第 9 行不起作用”到底是什么意思?那就是不绘图,它绘制错误,它崩溃,它在其他地方绘制
  • 它不显示更新的边框。它应该显示旋转板。
  • 电路板在第 8 行旋转,但新状态未显示,之前的情况仍然出现,直到第 4 行再次运行。
  • 在drawTable末尾添加drawnow

标签: matlab user-interface matlab-guide axes


【解决方案1】:

正如ThP 所说,我必须在drawTable 末尾添加drawow() 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    相关资源
    最近更新 更多