![1how to format a column of numbers into strings for ticklabel use - 2020 01 19.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/262496/1how%20to%20format%20a%20column%20of%20numbers%20into%20strings%20for%20ticklabel%20use%20-%202020%2001%2019.png)
how to format a column of numbers into strings for ticklabel use?
6 ビュー (過去 30 日間)
古いコメントを表示
I have a column of longitude values for the xtick of my plot:
xt = xticks(gca);
The numbers of xt can be -120, -110, -100, etc. I want to convert them to xt2: '120\circW', '110\circW', '100\circW', and then change the current xticklabel with these new labels, by using:
set(gca,'xtickLabel',xt2);
I know for an individual one, it can be done by:
xt2(i) = [num2str(xt(i)), '\circW'];
Is it possible to write a format change script without creating a loop?
Many thanks!
0 件のコメント
回答 (1 件)
Star Strider
2020 年 1 月 19 日
x = linspace(20, 50, 29);
y = sin((x-20)/30 * 2 * pi);
figure
plot(x, y)
xt = xticks(gca);
xt2 = compose('%.0f\\circW',xt);
set(gca,'xtickLabel',xt2);
producing:
![1how to format a column of numbers into strings for ticklabel use - 2020 01 19.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/262496/1how%20to%20format%20a%20column%20of%20numbers%20into%20strings%20for%20ticklabel%20use%20-%202020%2001%2019.png)
The compose function was introduced in R2016b.
The sprintfc funciton is undocumented. (Everyone has it.) The arguments are the same as for compose, so only the function name needs to change.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!