Set axes properties in a loop

5 Ansichten (letzte 30 Tage)
Robert Alvarez
Robert Alvarez am 16 Aug. 2016
Kommentiert: Robert Alvarez am 17 Aug. 2016
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?

Antworten (1)

Thorsten
Thorsten am 16 Aug. 2016
Bearbeitet: Thorsten am 17 Aug. 2016
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 Kommentare
Thorsten
Thorsten am 17 Aug. 2016
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 am 17 Aug. 2016
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by