Align text in two lines with respect to a location

Say, I have two strings 'a = 2' and 'bbb = 641', now I display them in a column using the command
display(['a = 2', char(10), 'bbb = 641'] )
it looks like
a = 2
bbb = 641
how can I align them with respect to '=':
a = 2
bbb = 641

 Accepted Answer

dpb
dpb on 1 Apr 2015
Edited: dpb on 1 Apr 2015
Use specific format string if you want a specific format...
>> fprintf('%8s = %4d\n','a',2, 'bbb',641)
a = 2
bbb = 641
>>
"More than likely you're using proportional fonts..."
The default is Helvetica. To see the issue, try
hT=text(0.4,0.2,{sprintf('%8s = %4d\n','a',2, 'bbb',641)});
for i=1:4
pause(0.75),set(hT,'fontname','Courier')
pause(0.75),set(hT,'fontname','Helvetica')
end
>>

5 Comments

Thanks for the response, however, it seems doesn't work in a universal manner. Say
test = sprintf('%8s = %4d\n','a',2, 'bbb',641);
text(0.1,0.6, test)
The printed texts are not aligned.
Depends on what you mean by "universal".
text isn't character-oriented but graphics.
For it use the 'horizontalalignment' property
See details under
doc text
for properties/examples...
Hi dpb, I tried all alignment methods for text object, still cannot get good result.
More than likely you're using proportional fonts...
Very good answer, thanks, dpb!

Sign in to comment.

More Answers (0)

Products

Asked:

on 1 Apr 2015

Commented:

on 16 Apr 2015

Community Treasure Hunt

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

Start Hunting!