2016-07-14 62 views
0

我在MATHLAB中使用GUI和Mfile编写代码。在我的程序中,我有一块板子,每次电脑放一块大理石,旋转板子,然后得到一个大理石的地方,并从用户旋转板子的方向,并在每个这些东西后,我希望它在GUI中显示axes1的结果。但是它在第9行中没有在下面的代码中工作:如何更新指南中的轴 - matlab

% 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 

你有什么想法吗?

它的旋转功能。 th板分为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 

+0

这是一个自定义旋转功能吗?如果是这样,请提供代码 – Suever

+0

你的意思是什么,但“它没有在第9行工作”?这不是情节,它绘制了错误的东西,它崩溃了,它绘制在其他地方 –

+0

它不显示更新的bord。它应该显示旋转的板。 – elsa

回答

0

ThP说,我必须在drawTable的末尾添加的DrawNow()函数。