Matlab 一次修改多个XTick值

Matlab 一次修改多个XTick值,matlab,matlab-figure,scatter-plot,Matlab,Matlab Figure,Scatter Plot,我想在Matlab版本R2014b中制作一个散点图矩阵,每个x轴和y轴只有两个最小和最大刻度,因为现实世界的示例有更多的图,这将更容易阅读。下面是一个示例,正如您所看到的,第二个for循环的工作方式与编辑刻度的目的不同。还有,有没有一种方法可以在没有for循环的情况下进行设置 % Example data example = [ -5.2844 5.0328 307.5500 -12.0310 5.1611 237.9000 -15.9510

我想在Matlab版本R2014b中制作一个散点图矩阵,每个x轴和y轴只有两个最小和最大刻度,因为现实世界的示例有更多的图,这将更容易阅读。下面是一个示例,正如您所看到的,第二个for循环的工作方式与编辑刻度的目的不同。还有,有没有一种方法可以在没有for循环的情况下进行设置

% Example data
        example =    [   -5.2844    5.0328  307.5500
  -12.0310    5.1611  237.9000
  -15.9510    7.5500  290.7600
  -11.5500   13.6850  285.8400
   -1.9356    9.4700  273.2600
   -9.2622    4.7456  232.6000
   -6.5639    5.2272  265.3800
   -4.4795   14.8320  281.1300
   -2.1474   13.0690  288.7300
   -3.7342   11.7450  265.2200
  -11.9040   10.9660  286.5700
   -2.1789    6.7442  258.9700
   -2.8316   11.8210  281.7500
   -2.6170    7.4740  244.8600
   -6.8770    1.6623  116.9800
  -10.2210    5.7575  300.2200
   -3.9430    5.9715  253.6000
   -3.3690    5.6530  230.9700
    2.6090    3.2750  236.8700
   -5.1430    8.4060  273.9600
   -7.9360    2.7855  254.8200
   -2.8665    2.0241  176.0600
   -0.0960    3.3165  228.6800
   -8.8465   10.3240  289.2100
   -8.1930   16.4070  289.7000
   -6.5840   13.4010  283.2600
    2.2405    8.4625  270.1000
   -2.2505    7.5555  285.9100
   -4.6955    6.2985  279.2000
   -0.7610   12.5210  283.2000
   -0.7510    9.9470  279.9100
    1.7120    8.5990  285.5700
   -0.6245    7.6905  251.9100
  -19.2320    6.8545  306.2700
   -4.1255    9.8265  298.2000
    2.9486    3.8881  250.2100
    1.7333    5.5000  240.7300
   -6.5881    3.9152  234.3800
   -7.9543    8.0771  276.8000
   -6.9641    8.8573  284.2800
  -10.3280    7.4768  291.8700
   -8.0818    5.1250  250.6600
  -10.1490    3.9427  271.0000
   -2.7786    3.7673  213.6500
  -14.5410   11.1500  288.9100
   -6.8118   11.0210  280.2000
   -2.6459    6.5127  213.2600
    1.4036    4.2023  253.9400
   -5.0014    9.3900  267.0600
   -9.7382   12.0990  290.8800]

% Labels for data
data_labels = {'Variable1','Variable2',...
    'Variable3'};

% Make scatterplot matrix with histogram on diagonal
figure()
[H,AX]= plotmatrix(example(:,:));

% label the axes of the plots (rotated) works fine
for i = 1:length(AX)
    ylabel(AX(i,1),data_labels{i},'Rotation',0,'HorizontalAlignment','right',...
        'FontSize',12);
    xlabel(AX(end,i),data_labels{i},'Rotation',60,'HorizontalAlignment','right',...
        'FontSize',12);
end

% Set ticks (not working yet)
NumTicks = 2;
for i = 1:length(AX)
    L = get(AX(i,i),'XLim');
    set(AX(i,i),'XTick',linspace(L(1),L(2),NumTicks));
    % set y ticks here too
end
在索引到AX时,您错误地使用了i或两个下标

此外,最好使用numel而不是length,并使用一个变量而不是i作为循环变量,因为这是0+1i的MATLAB内置变量。此外,您可以在AX中使用单个线性索引,而不是指定行和列下标

for k = 1:numel(AX)
    L = get(AX(k),'XLim');
    set(AX(k),'XTick',linspace(L(1),L(2),NumTicks));
end
如果您想在不使用循环的情况下执行此操作,您可以使用cellfun技术性地执行此操作,并且您可以使用以下语法在多个plot对象中设置相同的属性

set(array_of_plot_handles, {'Property'}, array_of_values_to_set)
应用到你的问题上,它会是

% Get all XLims as a cell array
oldlims = get(AX, 'XLim');

% Compute the new xlims
newlims = cellfun(@(x)linspace(x(1), x(2), NumTicks), oldlims, 'UniformOutput', false);

% Now set the values
set(AX, {'XTick'}, newlims);
或者如果你真的希望它是一行

set(AX, {'XTick'}, cellfun(@(x)linspace(x(1),x(2),NumTicks), get(AX, 'Xlim'), 'UniformOutput', false))

你需要使用AXi而不是AXi,我在你的最后一个循环中cellfun工作得很好!在您对原始“for”循环的编辑中,它仍然不太正确。。。也许是因为格塔西,我。。。需要更新吗?非常全面的示例+1
set(AX, {'XTick'}, cellfun(@(x)linspace(x(1),x(2),NumTicks), get(AX, 'Xlim'), 'UniformOutput', false))