Is there a way to measure interletter spacing for Fixed Width font in MATLAB?

7 vues (au cours des 30 derniers jours)
Kathleen Kay Amora
Kathleen Kay Amora le 27 Oct 2021
Commenté : Scott MacKenzie le 28 Oct 2021
Hello, I am trying to calculate the physical characteristics of the text we plan to use for our experiment. Now to finalize this, I need to know these following information PER character: size of letters, interletter spacing in cm and degrees of visual angle per font size (font size 16, 18, 20 and 22). I am relatively a newbie in MATLAB and I cannot find a function that could work. I am using Fixed Width font style.
I would appreciate any help I could get.
Thank you.

Réponses (2)

J. Alex Lee
J. Alex Lee le 27 Oct 2021
I have no idea what you mean by "visual angle per font size", but I know of one way:
lbl = uicontrol("Style","text","String","X","FontName","courier")
This creates a text label on a figure, and once it is rendered, you can query its "Extent" property:
lbl.Extent
which is a [position,extent] vector, so the 3rd element is the width.
So playing around with it can give hints about how the UI system will position characters
lbl = uicontrol("Style","text","String","X","FontName","courier").Extent
lbl = uicontrol("Style","text","String","XX","FontName","courier").Extent
lbl = uicontrol("Style","text","String","XX X","FontName","courier").Extent

Scott MacKenzie
Scott MacKenzie le 27 Oct 2021
Modifié(e) : Scott MacKenzie le 27 Oct 2021
For text fonts, 1 pt = 1/72 inches. So, if you set the font size of the text to, say, 36 pt., the character height will be 1/2 inch or 1.27 cm. I suggest you do some actual measurements to verify and work with those measurements. MATLAB's "extent" property for text includes margins, so I'd be hesitant to use those width and height values.
As for visual angle, that's an arctan calculation. You need the size of the characters and the distance of the participant's eye from the display. For example, the visual angle in degrees for a character 1 cm in size viewed from a distance of 65 cm is
atand(1/65)
ans = 0.8814
  2 commentaires
Kathleen Kay Amora
Kathleen Kay Amora le 28 Oct 2021
Hi Scott, thanks so much for this. Trying to run your first comment but I cannot view it in full. Can you put it in here again, it gets cut off at:
s = [s sprintf('%15s: %d
Scott MacKenzie
Scott MacKenzie le 28 Oct 2021
@Kathleen Kay Amora First, let me apologize for the false starts yesterday. I posted some code, then edited and reposted a few times. Then, I deleted the code as I felt it was heading down the wrong path. Since you've asked, I'll repost the code below. I hope you find it useful.
There are a few challenges in trying to use code to get the measurements you want. Many people's computing environments include different sizes of displays or >1 display, so the 'centimeter' units produced by the code are dubious at best. Plus, the text sizing measurements are for the text plus a margin around the text.
Here's the code. Good luck.
set(0, 'units', 'centimeters');
screenSize = get(0, 'ScreenSize'); % verify with measuring tape
wScreen = screenSize(3);
hScreen = screenSize(4);
txt = 'ABC';
fontSize = 48;
set(gca, 'xlim', [0 1], 'ylim', [0 1]);
t = text(0.5, 0.5, txt, 'FontName', 'fixedwidth', 'FontSize', fontSize, ...
'HorizontalAlignment','center', 'VerticalAlignment','middle', ...
'Margin', 15);
t.Units = 'centimeters';
d = 65; % distance (cm) of participant's eye from display (adjust as needed)
n = length(txt); % number of characters
w = t.Extent(3) / n; % per-character width (Note: includes margin)
h = t.Extent(4) / n; % per-character height (Note: includes margin)
va = atand(w/d); % per-character visual angle (degrees)
s = newline;
s = [s sprintf('%15s: %.1f cm x %.1f cm\n', 'Screen size', wScreen, hScreen)];
s = [s sprintf('%15s: %s\n', 'Text', txt)];
s = [s sprintf('%15s: %d pt\n', 'Font size', fontSize)];
s = [s sprintf('%15s: %.2f cm per character\n', 'Width', w)];
s = [s sprintf('%15s: %.2f cm per character\n', 'Height', h)];
s = [s sprintf('%15s: %.2f degrees per character at d = %d cm\n', 'Visual angle', va, d)];
disp(s);
Screen size: 49.3 cm x 30.8 cm Text: ABC Font size: 48 pt Width: 1.02 cm per character Height: 0.72 cm per character Visual angle: 0.90 degrees per character at d = 65 cm

Connectez-vous pour commenter.

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by