How to get xticklabels from xticks by merging cells?

조회 수: 4 (최근 30일)
Louis Tomczyk
Louis Tomczyk 2022년 8월 24일
댓글: Louis Tomczyk 2022년 8월 24일
Dear all,
I want to get my ticklabels from an array of numbers.
As the function is taking a cell-array, I want to create a function which enables the conversion of my array of numbers into a cell :
Xticks = [1,2,3];
Xtickslabels = tick2ticklabel(Xticks)
>>> Xtickslabels = {'1','2','3'}
I tried this:
Xticks = [1,2,3];
tmp = split(num2str(Xticks))
tmp = 3×1 cell array
{'1'} {'2'} {'3'}
tmp = reshape(tmp,1,length(tmp))
tmp = 1×3 cell array
{'1'} {'2'} {'3'}
But I am stuck here as I didn't find a way to merge the cells into one.
Thanks in advance.
Best regards,
louis

채택된 답변

Chunru
Chunru 2022년 8월 24일
plot(randn(7,1))
Xticks = 1:2:7; % for example
% It's simpler and neater to use string
Xtickslabels = string(Xticks)
Xtickslabels = 1×4 string array
"1" "3" "5" "7"
set(gca, 'XTick', Xticks, 'XTickLabel', Xtickslabels)

추가 답변 (2개)

Mathieu NOE
Mathieu NOE 2022년 8월 24일
hello
here you are my friend :
Xticks = [1,2,3];
tmp = num2str(Xticks);
Xtickslabels{1} = tmp;

Louis Tomczyk
Louis Tomczyk 2022년 8월 24일
Hello again,
Actually I used both of your answers @Chunru and @Mathieu NOE and thanks for it.
The function I created is then taking 2 arguments an array of doubles and the number of decimals we want.
function Ticklabels = ticks2ticklabels(ticks,decimals)
Ticklabels = string(ticks);
for k = 1:length(Ticklabels)
tmp = char(Ticklabels(k));
index_comma = strfind(tmp,'.');
if decimals == 0
Ticklabels(k) = tmp(1:index_comma-1);
else
Ticklabels(k) = tmp(1:index_comma +decimals);
end
end
Ticklabels = {Ticklabels}
end
It produces the desired output:
Xticks = logspace(1.5,5.5,20)
Xticklabels = tick2ticklabels(Xticks,0)
>> Xticks
Xticks =
1.0e+05 *
Columns 1 through 14
0.0003 0.0005 0.0008 0.0014 0.0022 0.0036 0.0058 0.0094 0.0153 0.0248 0.0403 0.0654 0.1062 0.1725
Columns 15 through 20
0.2801 0.4549 0.7386 1.1994 1.9475 3.1623
>> Xticklabels
Xticklabels =
1×1 cell array
{["31" "51" "83" "135" "219" "356" "579" "941" "1528" "2481" "4029" "6543" "10624" "17252" ]}
but just doing
figure
xticks(Xticks)
xticklabels(Xticklabels)
produces the error:
Error using xticklabels (line 43)
Specify tick labels as cell array of character vectors, string array, or one of the 'mode', 'auto', or 'manual' options.
How to solve it?
Thanks again,
louis
  댓글 수: 2
Mathieu NOE
Mathieu NOE 2022년 8월 24일
hello again
Xticklabels must not be a 1 x 1 cell but a 1 x 20 cell
making all this code for 1 x 1 cell is unneccessary
Louis Tomczyk
Louis Tomczyk 2022년 8월 24일
Ok but it does not change either.
I modified the function:
function Ticklabels = ticks2ticklabels(ticks,decimals)
Ticklabels = string(ticks);
for k = 1:length(Ticklabels)
tmp = char(Ticklabels(k));
index_comma = strfind(tmp,'.');
if decimals == 0
Ticklabels(k) = tmp(1:index_comma-1);
else
Ticklabels(k) = tmp(1:index_comma +decimals);
end
end
Ticklabels = cellstr(Ticklabels);
end
>> Xticklabels
Xticklabels =
1×20 cell array
Columns 1 through 13
{'31'} {'51'} {'83'} {'135'} {'219'} {'356'} {'579'} {'941'} {'1528'} {'2481'} {'4029'} {'6543'} {'10624'}
Columns 14 through 20
{'17252'} {'28013'} {'45487'} {'73861'} {'119935'} {'194748'} {'316227'}
But still nothing.
figure
plot(randn(1,20))
Xticks = logspace(1.5,5.5,20);
Xticklabels = ticks2ticklabels(Xticks,0);
xticks(Xticks)
xticklabels(Xticklabels)
By the way, why when I want to run the code in the browser it always says that "something went wrong. please try again"?
Thanks again!

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

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by