Many times I want to set the axes properties with a loop. Here is an example of how I am currently doing it:
ax = gca;
axs = 'XYZ';
for k = 1:3
ax.([axs(k) 'Label']).String = sprintf('A_%d (g/cm^2)',k); % label axis units
end
Is there a better way to do this?

답변 (1개)

Thorsten
Thorsten 2016년 8월 16일
편집: Thorsten 2016년 8월 17일

0 개 추천

In this case I think that the for loop is kind of overkill. Without a loop, it is much easier to understand what's going on:
ax.XLabel.String = 'A_1 (g/cm^2)';
ax.YLabel.String = 'A_2 (g/cm^2)';
ax.ZLabel.String = 'A_3 (g/cm^2)';

댓글 수: 3

That is a simplified excerpt to explain the problem. Here is the actual code:
Ap = @(x) interp1(1:50,nodelist(1,:),x,'linear','extrap');
axs = 'XYZ';
ax = gca;
ax.CameraPosition = [93.8319 -47.0997 260.6367];
for k = 1:3
axlbl = [axs(k) 'TickLabel']; % read current axes
lblidx = str2num(char(ax.(axlbl))); % convert to ints
lblidx(lblidx<1) = 1; % change to 1 ... so can use as indexes into vector
ax.(axlbl) = num2str(Ap(lblidx),'%0.2f'); % overwrite tick labels
ax.([axs(k) 'Label']).String = sprintf('A_%d (g/cm^2)',k); % label axis units
end
Thorsten
Thorsten 2016년 8월 17일
I see. Please post the complete example such that we can run the code. For example, nodelist is missing, and a command like plot or surf to show the data.
Robert Alvarez
Robert Alvarez 2016년 8월 17일
The exact code is not relevant. My point is that I want to do complicated stuff inside the loop and it is clumsy to have to repeat the code for each axis.
The use of separate function names such as xlabel, ylabel, zlabel or separate variable names like xticklabel, yticklabel, etc for analogous variables of the plot axes was a design decision by Mathworks that makes it hard to write code to do similar things to each axis.
I am hoping that there is some perhaps undocumented way to access these variables without having to got through the contortions in my example or simply repeating the code as you did in your example.

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

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

질문:

2016년 8월 16일

댓글:

2016년 8월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by