Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to determine size of checkbox

Asked by Jan on 20 May 2013

I am having some troubling positioning a checkbox in a UI. I have (programmatically) added a number of rows of checkboxes to a figure. On my PC with windows XP (Matlab 2011b) I see that these are alligned nicely. I use a height of 15 pixels (as also indicated in this thread: http://www.mathworks.com/matlabcentral/answers/10123-calculate-the-size-needed-for-a-uicontrol) E.g. 10 rows of checkboxes for a total vertical size of 150 pixels.

However, on another PC with Windows 7 (also 2011b), I see that the checkboxes overlap. They are not fully displayed. Some trial and error indicates that the size of these checkboxes is 18 pixels. Another difference I noted is that the ScreenPixelsPerInch property of the root object is set to 116. On the Windows XP computer it is set to 96. Could that explain the difference? At http://www.mathworks.com/matlabcentral/newsreader/view_thread/303963 someone has encountered the same difference in DPI setting. I can see that chaning the ScreenPixelsPerInch settings changes the font size and button size. The size of the checkbox seems unaffected.

My question: on what is the size of the checkbox control depending? How can I determine how much space I need to fit a number of checkboxes?

Update I'm now starting to wonder if this is something Windows 7 specific. I use the 'outerposition' property to place multiple figures next to each other without overlapping, but also not leaving any space between them. This works fine on Windows XP. However, when looking closely at the figures on my Windows 7 machine, I noticed the borders are larger and overlapping. Does Matlab on Windows 7 draw certain UI elements (e.g. borders, checkboxes) in a different way?

Another Update I think I'm getting closer to a solution. I found that the Windows display setting "text size DPI" has something to do with this. Normally, this settings is set to 100% (96 dpi). This is reflected in the ScreenPixelsPerInch attribute of the root object in Matlab and it gives a checkbox height of 15 pixels. I tried various settings and saw the following:

 Windows text size setting - Windows DPI - Matlab DPI - Checkbox size
 100 %                     - 96          - 96         - 15 pixels
 125 %                     - 120         - 116        - 18 pixels
 150 %                     - 144         - 116        - 22 pixels
 200 %                     - 192         - 116        - 22 pixels

As can be seen, for the 125% setting, Matlab scales nicely. For the 150% setting, the checkbox size is scaled properly (1.5 * 15 ~ 22). However, this is not reflected in the ScreenPixelsPerInch attribute of Matlab, which stays at 116.

Thus, I have an (partial) answer: use only 100 and 125 % settings in Windows. Does anybody know how to detect the large windows DPI settings (>125%)? It seems not detectable by looking at the ScreenPixelsPerInch attribute.

0 Comments

Jan

Products

3 Answers

Answer by Jan on 10 Jun 2013
Accepted answer

To answer my own question:

One can use the following code which uses only Matlab routines:

dpi = get(0, 'ScreenPixelsPerInch');
if dpi >= 116
  h = 18;
else
  h = 15;
end

For the cases I tested, the ScreenPixelsPerInch property only returns 96 or 116. This does not cover all cases. Therefore, the following solution uses a Java method:

dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
if dpi < 120
  h = 15;
elseif dpi < 144
  h = 18;
else
  h = 22;
end

During my testing, I only encountered these 3 checkbox sizes. The range of resolutions tested is between 96 and 288 DPI.

0 Comments

Jan
Answer by Jan Simon on 10 Jun 2013

The size of checkboxes and popupmenus is controlled by the operating system. While most of the settings can be determined by the Windows-API function GetSystemMetrics, finding out the exact dimensions is not trivial, see e.g. http://stackoverflow.com/questions/1164868/how-to-get-size-of-check-and-gap-in-check-box. Whenn different OS', fonts and screen resolutions come into play, a 100% exact solution is not possible in my opinion.

3 Comments

Jan on 10 Jun 2013

Granted, I did not test all possible variations in operating systems etc. At least this solution works for my current application.

Would I need a mex function to access the Windows API you mention?

Malcolm Lidierth on 12 Jun 2013

@Jan and/or Jan (?)

Nor sure whether you are using MATLAB uicontrols or Java/javacomponent.

"The size of checkboxes and popupmenus is controlled by the operating system". Not quite. Uicontrols in MATLAB are Java Swing objects - so controlled by the Java look-and-feel. While that will be OS-dependent unless altered from the default, the L&F is not restrained to respect OS defaults.

For better consistency between Windows XP/7 you could use the Nimbus L&F if that is acceptable - but I would change the L&F as early after MATLAB startup as possible: Java allows the L&F to be set at JVM startup - if changed after that using the Java UIManager, problems can arise when the wrong L&F UI delegate is called. You may get away with it - but there will always be a risk of a subsequent run-time exception.

get(0, 'ScreenPixelsPerInch') returns the wrong answer on some MATLAB versions when on some OSs (~R2010/R2011 - I forget the details). java.awt.Toolkit.getDefaultToolkit().getScreenResolution() will give the correct answer, but not necessarily the one used by all MATLAB TMW code.

Expect a difference in pixel position when comparing MATLAB and Java positions. I suspect out-by-1 errors converting between MATLAB (unity-based) and Java (zero-based) positions in some TMW code, but those can add up to greater errors through multiple calls. Some of TMWs mouse position code allows a +/-5 pixel tolerance.

This can be an issue with using javacomponent - the MATLAB hgcontainer and the AWT/Swing component may be offset from each other.

While ScreenPixelsPerInch is settable in the MATLAB root- but does that really make any sense unless you also change monitors/settings?

Regards

Malcolm

Jan Simon on 12 Jun 2013

@Jan: Yes, the Windows-API function can be called through a MEX functions.

@Malcolm: You are right. I was too sloppy with the claim, that Java is a part of the OS already.

Jan Simon
Answer by Jan Simon on 12 Jun 2013

Do it the hard and reliable way: Create a checkbox, capture the screen, use the image processing toolbox to find the edges. Finding the top-left and bottom right pixels with a color different from the figures background should even work without the IPT.

0 Comments

Jan Simon

Contact us