MATLAB 中的正方形图形

Graph in a square in MATLAB

我有一个包含 7 行的矩阵,我会自动上传它并生成一个图表。我无法弄清楚,我如何才能在正方形中准确地表示它。我用矩阵制作的图表如下所示:

这是显示我文件中矩阵的代码:

reverse_matrix = flipud(matrix_to_display)
imagesc(reverse_matrix) 
set(gca,'XTickLabel',{'2','4', '6', '8', '10', '12', '14'})
set(gca,'YTickLabel',{'33.60 cm','29.25 cm', '24.90 cm', '20.55 cm', '16.20 cm', '11.85 cm', '7.50 cm'})

如何把矩阵做的图形放在白色方块的地方?

这就是从文件创建矩阵的代码

    array_step_one = zeros(1,14)
    array_step_two = zeros(1,14)
    array_line_matrix = zeros(1,14)
    final_matrix = cell(7)
    for index = 1:14
file_path = strcat('C:/StefanMatlab/1-4w/file', num2str(index))
file_path = strcat(file_path, '.txt');

fd_r = fopen(file_path, 'r')

content_info = fgets(fd_r)
content_info = fgets(fd_r)
content_info = fgets(fd_r)
content_info = fgets(fd_r)
content_info = fgets(fd_r)
content_info = fgets(fd_r)
temp_array = fscanf(fd_r, '%i')
for i = 1:14
    if mod(index,2) == 1
        array_step_one(i) = temp_array(i)
    else
        array_step_two(i) = temp_array(i)
    end
end
if mod(index , 2) == 0
    for j = 1 : 14
        if array_step_one(j) >= array_step_two(j)
            array_line_matrix(j) = array_step_one(j)
        else
            array_line_matrix(j) = array_step_two(j)
        end
    end
    array_step_one = zeros(1,14)
    array_step_two = zeros(1,14)
    for k = 1 : 14
        final_index = index / 2
        final_matrix{int16(final_index), k} = array_line_matrix(k)      
    end
end
    fclose(fd_r)
    end

像下面这样的简单解决方法怎么样?

matrix_to_display = ones(9,9,3);
a = rand(7,7,3);
matrix_to_display(2:8,2:8,:) = a;

reverse_matrix = flipud(matrix_to_display);
imagesc(reverse_matrix); 

set(gca,'XTickLabel',{'2','4', '6', '8', '10', '12', '14'});
set(gca,'YTickLabel',{'33.60 cm','29.25 cm', '24.90 cm', '20.55 cm', '16.20 cm', '11.85 cm', '7.50 cm'});

输出:

更新:

检查以下代码。

clear all;

matrix_to_display = rand(7,7,3);

xlim = [0 8] ;
ylim = [0 8] ;
[X,Y] = hatch_coordinates( xlim , ylim , .1, .1) ;
plot(X,Y,'k');
hold on ; grid off

reverse_matrix = flipud(matrix_to_display);
img = image(reverse_matrix); 

p = patch([.5 7.5 7.5 .5],[.5 0.5 7.5 7.5],'r');
set(p,'FaceAlpha',0);

输出:

更新二:

考虑到 Hoki 的建议,我更新了代码如下。

clear all;

matrix_to_display = rand(7,7);

xlim = [0 8] ;
ylim = [0 8] ;
[X,Y] = hatch_coordinates( xlim , ylim , .1, .1) ;
plot(X,Y,'k');
hold on ; grid off

reverse_matrix = flipud(matrix_to_display);
img = imagesc(reverse_matrix); 
colormap('gray');

p = patch([.5 7.5 7.5 .5],[.5 0.5 7.5 7.5],'r');
set(p,'FaceAlpha',0);

set(gca,'XTickLabel',{'1', '2','4', '6', '8', '10', '12', '14', '15'})
set(gca,'YTickLabel',{'37.95 cm','33.60 cm','29.25 cm', '24.90 cm', '20.55 cm', '16.20 cm', '11.85 cm', '7.50 cm', '3.15 cm'})

输出:

Hwathanie 暗示的解决方案实际上是最简单的实现方法,如果您只有 1 个图像数据矩形要绘制,并且您可以通过重新调整 XY 刻度标签来实现在轴标签上显示您想要的内容。

在他更正他的代码之前,如果你想让它工作,玩一下他的第一个代码和第二个代码(看看我对他的回答的评论,所有这些你应该有所了解)。

我采用了另一种更复杂的方法,但它的好处是更具可扩展性。基本上,我创建了一个网格化的补丁,其中包含与矩阵中的点一样多的面孔,然后我为每个面孔着色,因为 imagesc 会为图像着色。这个 patch 对象具有可移动和可缩放的优点,因此我可以直接将它插入到您想要的 "rectangle" 位置,就像您在之前的

中想要的那样
%// sample matrix data
nl = 7 ; nc = 14 ; nColor = 16 ;
cmap = gray(nColor) ;                   %// Pick a gray colormap (16 colors)
matrix_to_display = rand( nl , nc ) ;   %// random colors matrix
reverse_matrix = flipud(matrix_to_display);
figure ; colormap(cmap) ;               %// assign the gray colormap to the figure

%// Get the hatched background
xlim = [0 61] ; ylim = [0 45] ;
[X,Y] = hatch_coordinates( xlim , ylim ) ; %// this return coordinates to plot a hatch pattern
plot(X,Y,'k')                              %// and this simply plot the pattern, with the attributes you want (color, linespec, etc ...)
hold on ; grid off

%// *** THIS IS THE INTERESTING BIT ***
%// Define your rectangle patch 
pos = [3.75 5.6 53.5 29.5];  %// [x0 y0 W H] of your rectangle

fv = meshpatch( size(reverse_matrix) ) ;                            %// get the basic patch face grid
fv.vertices = bsxfun( @times , fv.vertices , [pos(3) pos(4) 1] ) ;  %// apply scale factor to vertices coordinates
fv.vertices = bsxfun( @plus , fv.vertices  , [pos(1) pos(2) 0] ) ;  %// apply offset to vertices coordinates

PatchFacesColors = rot90(reverse_matrix,-1) ;                       %// necessary to adjust color index to patch faces organisation
fv.facevertexcdata = PatchFacesColors(:) ;                          %// add "facevertexcdata" to the patch structure
hp = patch(fv,'FaceColor','flat','CDataMapping','scaled','EdgeColor','none') ; %// create the patch

%// refine axis
axis([0 61 0 45])
yt = [0 7.5 11.85 16.2 20.55 24.9 29.25 33.6 39.3 45].' ;
set( gca , 'Ytick' , yt , 'YTickLabel' , num2str(yt) )


代码:

对于函数 hatch_coordinates.m 可以在这个答案中找到:

对于函数 meshpatch.m 是:

function fv = meshpatch( meshdim , normalized )
%// function fv = meshpatch( meshdim , normalised )
%//
%// return patch structure f.faces and f.vertices of a patch having the same
%// number of faces than the input size.

if nargin < 2 ; normalized = true ; end %// default option

nfx = meshdim(2) ; %// number of faces on X
nfy = meshdim(1) ; %// number of faces on X

%% // build patch vertices
x = (0:nfx).' * ones(1,nfy+1) ;    %'//ignore this comment
y = ones(nfx+1,1) * (0:nfy) ;
if normalized
    x = x ./ nfx ;
    y = y ./ nfy ;
end
fv.vertices = [x(:) y(:) zeros(numel(x),1) ] ;

%% // build patch faces
f0 = [1 2 nfx+3 nfx+2].' ;              %'// basic patch cell
fl = f0 * ones(1,nfx*nfy) ;             %// replicate to create all patch cell

%// now adjust the vertex indices to get all the cells rigth
cellAdd = bsxfun(@plus,(1:nfx).'*ones(1,nfy),(0:nfx+1:nfy*nfx))-1 ; 
fv.faces = bsxfun(@plus,fl, cellAdd(:).' ).' ; %// adjust indices of patch cell