当使用逗号作为小数时,Matlab绘图在图中缺少角点值

时间:2017-12-08 22:34:21

标签: matlab

要更改小数。在图中的x轴和y轴上,我使用此函数:

function decimal_comma(axis_handle, axis_name, varargin)

  switch axis_name
    case 'XY'
        decimal_comma(axis_handle, 'X', varargin{:});
        decimal_comma(axis_handle, 'Y', varargin{:});

    case {'X', 'Y'}
        tick = get(axis_handle, strcat(axis_name, 'Tick'));

        n = length(tick);
        labels = cell(1,n);

        for i = 1:n
            label = num2str(tick(i), varargin{:});
            labels{i} = strrep(label, '.', ',');
        end

        labels{1} = '';
        labels{n} = '';

        set(axis_handle, strcat(axis_name, 'TickLabel'), labels);

    otherwise
        error('Wrong axis name! Use one of X, Y or XY.');
  end
end

我的情节功能:

data = load ('w3.mat');
plot(data.w3(:,1), data.w3(:,5),'g',data.w3(:,1), data.w3(:,4),'k'...
,data.w3(:,1), data.w3(:,7),'m')
hold on
x1 = data.w3(:,1);
y1 = data.w3(:,5);
y2 = data.w3(:,4);
y3 = data.w3(:,7);
err1 = 0.009*ones(size(y1));
err2 = 0.009*ones(size(y2));
err3 = 0.018*ones(size(y3));
errorbar(x1, y1, err1,'Color','green');
errorbar(x1, y2,err2,'Color','black');
errorbar(x1, y3, err3, 'Color','magenta');
lgd = legend('transmissie','reflectie','samen','Location','west','Orientation','verticol');   
title(lgd,'Legenda')
title('gefolariseerd onder 0 graden ')
xlabel('hoek van inval');
ylabel('intensiteit');
decimal_comma(gca, 'X', '%.0f');
decimal_comma(gca, 'Y', '%.1f');

当我运行此功能时,会显示以下图表

The plot

在此图中,缺少角点值,我想让它们可见,并且在轴中仍然使用逗号作为小数。

1 个答案:

答案 0 :(得分:1)

问题是由当前(defaultized)xticks / yticks设置引起的。您必须与它们一起玩才能获得角标记。或者,尝试操纵xlimylim值,直到获得符合您需求的结果。

无论如何,你改变逗号分隔符的方法对我来说看起来有点棘手......我建议你以下一个:

plot(x,y);
ax = gca;
ChangeDecimals(ax,'%.0f','X');
ChangeDecimals(ax,'%.1f','Y');

function ChangeDecimals(ca,fmt,opt)
    if (nargin < 1)
        ca = gca;
    end

    if (nargin < 2)
        fmt = '%.1f';
    end

    if (nargin < 3)
        opt = 'Both';
    end

    % reformat X-Axis ticks...
    if (strcmp(opt,'Both') || strcmp(opt,'X'))
        ticks = get(ca,'XTick'); 
        lbls = arrayfun(@(x){strrep(sprintf(fmt,x),'.',',')},ticks);
        set(ca,'XTickLabel',lbls);
    end

    % reformat Y-Axis ticks...
    if (strcmp(opt,'Both') || strcmp(opt,'Y'))
        ticks = get(ca,'YTick'); 
        lbls = arrayfun(@(x){strrep(sprintf(fmt,x),'.',',')},ticks);
        set(ca,'YTickLabel',lbls);
    end
end