问题
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