User Tools

Site Tools


en:software:matlab:trepr:dev:gui:fonts

Fonts

A few words about fonts, how to use fonts that look basically the same on all three major platforms, and how to adjust font sizes properly.

Although Matlab™ claims to be rather platform independent when it comes to GUI programming, it actually is not really if you deal with fonts.

  • How to use cross-platform monospaced fonts
  • How to use cross-platform font sizes
  • How to deal with special characters

How to use cross-platform monospaced fonts

The default and preferred way in Matlab to display text as monospaced (sometimes called “typewriter font”) is to set the “FontName” property to “FixedWidth”.

Actually, that does not work with Linux computers, as the default font name connected to “FixedWidth” converts to “Courier New” on all platforms, but seems not to be available with Linux platforms.

To check your platform's setting of the default fixed font, you may use

get(0,'FixedWidthFontName')

At least, using the fontname “FixedWidth” does not result in any error with the Linux platform, Matlab just doesn't show the text as monospaced.

The workaround: Use “Monospaced” as fontname instead. That seems to work on all three platforms, although at least on Mac OS X the “Monospaced” font is narrower than the “FixedWidth” font.

How to use cross-platform font sizes

It appears that different platforms have their complete interpretation of the font sizes, even if they are provided in pixels (“px”) as unit (that should be the same relative to all the other controls on every platform).

It has turned out to be a good compromise to set the “Unit” to “Pixels” for every uicontrol, and the “FontSize” property to “12” whereever necessary.

That should give you a somewhat consistent font size across the platforms. Nevertheless, check carefully that your text fits well inside the box on every platform. On the Linux platform, text seems to be slightly bigger, therefore you might be on the save side if you develop on a Linux box and the text fits there. But this is no guarantee whatsoever.

How to deal with special characters

One way to include special characters is to make use of the char() function in Matlab.

But beware of the inherent limitations of that function, as the Matlab™ help clearly states:

S = char(X) converts array X of nonnegative integer codes into a character array. Codes from 0 to 127 correspond to ASCII characters, which are uniform across systems. The characters that correspond to higher codes depend upon your current locale setting […], and codes greater than 65535 are not defined. Matlab™ help for the char command, © The MathWorks, Inc..

Therefore, although not strictly depending on your platform (aka operating system) it well depends on the locale settings of the platform.

One way to get rid of that platform dependence is to use HTML and its capability of coding most of the often used special characters as “named HTML signs”. Limitations here are that there is no easy way to use HTML for either text labels or edit boxes. To use HTML for these two occations (where they might be most useful often), you need to access the underlying Java Swing objects.

See Yair Altman's blog "Undocumented Matlab" for how to do that:

A simple example for using text labels via Java will follow shortly.

en/software/matlab/trepr/dev/gui/fonts.txt · Last modified: 2020/09/30 21:35 by 127.0.0.1