필터 지우기
필터 지우기

Display comma as decimal digits separator in simple 2D plots

조회 수: 24 (최근 30일)
Elia Sironi
Elia Sironi 2019년 11월 15일
댓글: Walter Roberson 2021년 12월 23일
Is it possible in a simple 2D plot display numbers with comma as decimal digits separator instead of the dot?
Maybe is there a label to add in the xtickformat command?

채택된 답변

Walter Roberson
Walter Roberson 2019년 11월 15일
No, the only way to do that is to build a TickLabels cell array of character vectors, or string object array, containing the characters you want to be displayed.
  댓글 수: 3
Karsten Opel
Karsten Opel 2021년 12월 22일
This works fine, but be aware of a possible pitfall: If you resize your figure causing a change in the number of ticks, the tick labelling will not reflect these changes correctly.
Walter Roberson
Walter Roberson 2021년 12월 23일
@Karsten Opel is correct: the above code changing the XTickLabels property needs to be done each time the number or position of ticks is changed. If you are working with tradtional figures, this might involve using a SizeChangedFcn (or ResizeFcn) callback at the figure or uipanel level; unfortunately there is no similar callback at the axes level.
To work at the axes level, you might need to do something like add a listener for PostSet on the XTicks property -- or perhaps on the XRuler Ticks property.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Roofus Milton
Roofus Milton 2019년 11월 15일
This is possible by calling the .NET Framework string formatting functions. Without getting into a discussion of Cultures in the Framework, I have provided a simple function which provides the format.
NumberFormatMatlabAnswers([1000:1010]', 4, 'decimalSeparator', ',', 'numberGroupSeparator', '.')
function output = NumberFormatMatlabAnswers(nums, varargin)
% initialize the inputParser
ip = inputParser;
% add function parameters to inputParser
addRequired(ip, 'nums');
addOptional(ip, 'decimals', 2);
addParameter(ip, 'decimalSeparator', '.');
addParameter(ip, 'numberGroupSeparator', ',');
% validate the parameters
parse(ip, nums, varargin{:});
% create the .NET object to store format specifics
numFormatInfo = System.Globalization.NumberFormatInfo();
% pass the custom format specifics to their respective object properties
numFormatInfo.NumberDecimalSeparator = ip.Results.decimalSeparator;
numFormatInfo.NumberGroupSeparator = ip.Results.numberGroupSeparator;
% initialize the output cell array
output = cell(length(ip.Results.nums), 1);
% create the format string for .NET
formatString = strcat('{0:N', num2str(ip.Results.decimals), '}');
for p = 1:length(ip.Results.nums)
% get the formatted string from .NET
output{p} = char(System.String.Format(numFormatInfo, formatString, ip.Results.nums(p)));
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 11월 15일
And you would then have to pass output as the appropriate tick labels -- the above does not change how axes tick labels are constructed.

댓글을 달려면 로그인하십시오.


Elia Sironi
Elia Sironi 2019년 11월 15일
Thank you to both of you. Walter's suggestion does exactly what I was searching!

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by