MATLAB Answers

How can I use the "\t" format in a listbox in MATLAB to place a tab in a string expression?

133 views (last 30 days)
I would like to be able to add a tab to a string with the "\t" format rather than single spaces.
Here is an example:
bas{1}.string=sprintf('\t\thello\tagain');
textbox=uicontrol('Units','normalized',...
'max',2,...
'FontName','arial','FontSize',12,...
'Style','listbox','Selected','on',...
'Position',[0 .5 .5 .5],'Value',1);
set(textbox,'String',bas{1}.string);
The example only produces empty square boxes in place of the "\t".

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to use the "\t" format in a listbox in MATLAB to place a tab in a string expression is not available.
As a workaround, you can use spaces along with a fixed-width font to align the columns.
figure
h1 = uicontrol(gcf, 'style', 'listbox', ...
'position', [100 100,300,100], ...
'background', [1 1 1] );
data1 = 1:0.1:3;
data2 = 10:1:30;
for k = 1:21
str(k) = {sprintf('%2i %5.2f %3i', k, ...
data1(k), data2(k))};
end
set(h1, 'string', str )
set(h1,'FontName','FixedWidth')
If you want text to use a fixed-width font that looks good in any locale, you should set FontName to the string FixedWidth
set(text_handle,'FontName','FixedWidth')
This eliminates the need to hardcode the name of a fixed-width font, which may not display text properly on systems that do not use ASCII character encoding (such as in Japan where multibyte character sets are used). A properly written MATLAB application that needs to use a fixed-width font should set the "FontName" property to "FixedWidth" (note that this string is case sensitive) and rely on the "FixedWidthFontName" property to be set correctly in the end-user's environment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!