matlab: variable horizontal alignment of text

时光总嘲笑我的痴心妄想 提交于 2021-01-27 21:24:23

问题


Text objects in MATLAB contain a horizontal alignment property, which can be assigned a value of left, center, or right. Attempts to assign this property by a vector of alignments of equal length to the vectors of strings and coordinates fails to give the intended behavior.

For instance, a statement of the form :

text([1,1,1]/4,[1,2,3]/4,{'ABC';'BCD';'CDE'})

displays the contents of a length-3 cell array of char objects at the X- and Y-coordinates specified by length-3 double arrays. However, attempting to introduce a length-3 cell array of char objects for independent specification of the horizontal alignment of each text element is syntactically invalid;

e.g.,

text([1,1,1]/4,[1,2,3]/4,{'ABC';'BCD';'CDE'},'HorizontalAlignment',{'left';'center';'right'})

My question concerns whether it is possible to specify the HorizontalAlignment property of MATLAB text objects in a variable manner without resorting to constructs explicitly involving loops and conditionals.


回答1:


You can't assign multiple property values upon creation, but once you have a vector of handles, you can use the many-to-many form of set() like so:

h = text([1,1,1]/4, [1,2,3]/4, {'ABC';'BCD';'CDE'});
set(h, {'HorizontalAlignment'}, {'left';'center';'right'});

The value array has one row per object, one column per property.



来源:https://stackoverflow.com/questions/21440457/matlab-variable-horizontal-alignment-of-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!