Search code examples
matlabscreen-resolutionuicontrol

Matlab UIControl Units


I am using uicontrol to create GUI Elements. Following is my Code

uicontrol('Style','pushbutton', 'String','Load data','Parent',hTabs(1),'Position',[250 825 80 20], 'Callback',@ButtonCallback);

Here the problem is when i am using Units normalized option the GUI element is disappearing from Screen. I want to use normalized so that GUI adjusts itself in different screen resolutions. Any idea on this would be very helpful for me.


Solution

  • When you use Normalized units you need to define the position between 0 and 1, with 0 being the bottom/left hand side and 1 being the total height/width of the containing object.

    You are currently defining the position using numbers that are well out of this range. You can do two things.

    • Switch the units to normalized in seperate function call (demonstrated below)
    • Create the uicontrol with normalized units, but you'll have to calculate the proper position vector

    Here are examples on how to do either

    A simple solution is to create the uicontrol and then set the units to normalized in a separate call

    u = uicontrol(...) %don't specify the units
    set(u,'Units', 'Normalized'); % this solves your problem
    

    If you want to get the position vector in normalized units

    normPos = get(u, 'Position') % get the position in normal space
    

    Then use these numbers to create the uicontrol with normalized units:

    u = uicontrol(...,'Units','Normalized', 'Position', normPos);