Is there a groot default for xlabel and ylabel HorizontalAlignment and VerticalAlignment?

4 ビュー (過去 30 日間)
I'm trying to do something global like
set(groot, 'DefaultXLabelVerticalAlignment', 'middle').
I looked through the entire groot 'factory' and saw nothing appropriate. I expressly don't want to have to set this everytime I call xlabel. Any ideas?
  2 件のコメント
Jakob B. Nielsen
Jakob B. Nielsen 2020 年 1 月 28 日
Cant you just define your own xlabel function e.g. Myxlabel, in which you call xlabel as you would but also hardset the alignment that you want? That way you just need to call Myxlabel instead of xlabel, and otherwise do things as normal.
Etaoin Shrdlu
Etaoin Shrdlu 2020 年 1 月 28 日
Thanks, Jakob. This would be fine for one program, but I have a suite of dozens of programs that have a common header file in which I set up default UI and Axes properties (e.g. fonts). I want to avoid having to edit all these programs, but just add one line to the header file. However, no such luck apparently.

サインインしてコメントする。

採用された回答

Etaoin Shrdlu
Etaoin Shrdlu 2020 年 1 月 28 日
Finally, this piece of code works (R2016a). I put it in the header file that is called by all GUI programs in my suite:
set(groot, 'DefaultAxesCreateFcn', @axisDefaultCreateFcn);
function axisDefaultCreateFcn(hAx, ~)
xl = get(hAx, 'XLabel');
set(xl, 'VerticalAlignment', 'middle');
end

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2020 年 1 月 28 日
There does not appear to be a way to specify it down that far.
You can get as far as specifying DefaultAxesXLabel which you would set to a text() object, but the entire object would get overwritten if you used xlabel() rather than the property being copied from the default.
You can set the axes or figure DefaultTextVerticalAlignment property, but that would apply to all text() objects under the container.
  2 件のコメント
Etaoin Shrdlu
Etaoin Shrdlu 2020 年 1 月 28 日
Thanks, Walter. Unfortunately, xlabel appears to be a law unto itself. Whereas the default VerticalAlignment for text objects is 'middle', the default VerticalAlignment for xlabel is 'top', and I can't change it:
>> set(groot,'DefaultTextVerticalAlignment', 'middle');
>> get(xlabel('Hello'), 'VerticalAlignment')
ans =
'top'
Walter Roberson
Walter Roberson 2020 年 1 月 28 日
I just looked at the code for xlabel, and I see that it does not itself set the VerticalAlignment: it inherits whatever VerticalAlignment was associated with the default XLabel from when the object was created.
Your test posted, in the form posted, does not establish positively, as default properties only apply to objects created after that point. xlabel() does not create an an XLabel, only sets the String property (and, typically, also FontSizeMode auto, FontUnitsMode auto, and copies FontWeight and FontAngle and FontName from the axes.) You would need to not have a current axes for xlabel() to be applied to in order to make the test fair. That said, I do replicate your results when no graphics objects exist yet.
Setting the groot DefaultAxesXLabel to a text() object does work for axes created after that point. But you do have to be careful that the text object is not deleted, which is a bit of a nuisance.

サインインしてコメントする。


Etaoin Shrdlu
Etaoin Shrdlu 2020 年 1 月 28 日
Excellent observation, which leads to this possible solution: Make each axis do the modification of its XLabel:
>> set(groot, 'DefaultAxesCreateFcn', 'set(get(gca, ''XLabel''), ''VerticalAlignment'', ''middle'')');
Kinda-sorta works as long as you create the axis first.

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by