2017-03-07 46 views
0

假設我有一個5x5矩陣。 矩陣的元素每秒都會改變(刷新)。矩陣值

我想能夠顯示矩陣(而不是作爲一個顏色表,但在網格中的實際值)是實時觀看改變它隨着時間的推移值。

我怎麼會去在MATLAB這樣做?

回答

2

clcdisp是最簡單的方法(由Tim爲應答)的組合,這裏就是你可能看中的,這取決於你一個「漂亮」的方法需要。這不會那麼快,但是您可能會發現一些好處,例如不必清除命令窗口或能夠對顏色進行編碼並保存無花果。

使用dispMatrixInFig(代碼在這個答案的底部),您可以在每個階段的圖形窗口(或獨特的數字窗口)中查看矩陣。

實例測試代碼:

fig = figure; 
% Loop 10 times, pausing for 1sec each loop, display matrix 
for i=1:10 
    A = rand(5, 5); 
    dispMatrixInFig(A,fig) 
    pause(1) 
end 

輸出爲一個迭代:

matrix in fig

評論功能代碼:

function dispMatrixInFig(A, fig, strstyle, figname) 
%% Given a figure "fig" and a matrix "A", the matrix is displayed in the 
% figure. If no figure is supplied then a new one is created. 
% 
% strstyle is optional to specify the string display of each value, for 
% details see SPRINTF. Default is 4d.p. Can set to default by passing '' or 
% no argument. 
% 
% figname will appear in the title bar of the figure. 

    if nargin < 2 
     fig = figure; 
    else 
     clf(fig); 
    end 
    if nargin < 3 || strcmp(strstyle, '') 
     strstyle = '%3.4f'; 
    end 
    if nargin < 4 
     figname = ''; 
    end 

    % Get size of matrix 
    [m,n] = size(A); 

    % Turn axes off, set origin to top left 
    axis off; 
    axis ij; 
    set(fig,'DefaultTextFontName','courier', ... 
      'DefaultTextHorizontalAlignment','left', ... 
      'DefaultTextVerticalAlignment','bottom', ... 
      'DefaultTextClipping','on'); 
    fig.Name = figname;  
    axis([1, m-1, 1, n]); 

    drawnow 
    tmp = text(.5,.5,'t'); 
    % height and width of character 
    ext = get(tmp, 'Extent'); 
    dy = ext(4); 
    wch = ext(3); 
    dwc = 2*wch; 
    dx = 8*wch + dwc; 

    % set matrix values to fig positions 
    x = 1; 
    for i = 1:n 
     y = 0.5 + dy/2; 
     for j = 1:m 
      y = y + 1; 
      text(x,y,sprintf(strstyle,A(j,i))); 
     end 
     x = x + dx; 
    end 

    % Tidy up display 
    axis([1-dwc/2 1+n*dx-dwc/2 1 m+1]); 
    set(gca, 'YTick', [], 'XTickLabel',[],'Visible','on'); 
    set(gca,'XTick',(1-dwc/2):dx:x); 
    set(gca,'XGrid','on','GridLineStyle','-'); 

end 
+0

尼斯一個...另一種方法可能是'openvar (「A」)',其顯示能夠順利通過的任何變化自動更新以'A' ...,看一下[linkdata](https://www.mathworks.com/help/matlab/ref/linkdata陣列編輯器。 HTML)可能也很有趣,如果願意更新一些情節,而不是可視化的值 – CitizenInsane

+0

我試圖操縱th e數組編輯器通過'v = desktop.getGroupContainer('Variables');'或'v = com.mathworks.mlservices.MLArrayEditorServices'命令編寫,但除了使數組版本只讀或不使用,這太難以取代變量的解除版本在它自己的身影通過命令 – CitizenInsane

+0

@CitizenInsane,我在剛剛開放的變量運行一對夫婦的快速測試,但它似乎並不可靠地更新每次迭代。 – Wolfie

2

我還以爲你可以用disp實現這一目標:

for i=1:10 
    A = rand(5, 5); 
    disp(A); 
end 

如果你的意思是你不希望在控制檯彼此頂部反覆輸出,可以包括clc清除各disp呼叫前控制檯:

for i=1:10 
    A = rand(5, 5); 
    clc; 
    disp(A); 
end 
0

如果你想上一個數字顯示您的矩陣這很容易。只需製作一個轉儲矩陣並顯示它。然後使用文本功能在圖上顯示您的矩陣。例如

randMatrix=rand(5); 
figure,imagesc(ones(20));axis image; 
hold on;text(2,10,num2str(randMatrix)) 

enter image description here

如果你想要做一個for循環和看到數字的變化,試試這個:

for i=1:100; 
    randMatrix=rand(5); 
    figure(1),clf 
    imagesc(ones(20));axis image; 
    hold on;text(2,10,num2str(randMatrix)); 
    drawnow; 
end 
+0

很短,您需要在'num2str'內爲字符串添加一些格式(https://uk.mathworks.com/help/matlab/ref/num2str.html#btf97wr),以便每列的寬度相同,並且如此一致。 – Wolfie