convert normal GUI cordinates to normalized cordinates

6 views (last 30 days)
How to convert normal GUI cordinates to normalized cordinates.
How to write a function for that?
Adam Danz
Adam Danz on 11 May 2021
Then the solution in my answer should work. Have you tried it?

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 9 May 2021
Edited: Adam Danz on 9 May 2021
The best solution would be to do the manual labor of changing the units for each component in the code that creates the GUI. It's as simple as copy/pasting this into each uicontrol line, if you're using that method to contruct the GUI, or as simple as setting the units to all components within GUIDE: ...,'Units','Normalize',.... It would take you less time to do that than the time you've waited to get this answer 馃榾.
But if you must do it programmatically, place this in your startup function or after all components have been generated. Not only does it change units to your selected units but it also returns all position vectors. If you want to remove that feature, remove lines that are followed by the comment "% POSITION DATA".
% Specify output Units
outUnits = 'normalized';
% Get figure handle
% Put all GUI object handles in a vector.
figChil = ________________; % VECTOR OF GUI OBJ HANDLES
% Eliminate objects that do not have a position or units property
isOK = isprop(figChil,'Position') & isprop(figChil,'Units');
figChil(~isOK) = [];
% loop through each object and *try* to temporarily change
% units to selected units. If the unit-change fails, the object
% will be skipped. Some objects in some releases of Matlab
% do not accept all units.
% objPos is an nx2 cell array where col 1 contains object handles
% and col 2 contains 1x4 position properties in your specified units.
objPos = [num2cell(figChil(:)),cell(numel(figChil),1)]; % POSITION DATA
pass = true(numel(figChil),1); % POSITION DATA
for i = 1:numel(figChil)
figChil(i).Units = outUnits;
objPos{i,2} = figChil(i).Position; % POSITION DATA
pass(i) = false; % POSITION DATA
To see the objects that have been converted to your specified units, this section places a red rectangle around all objects using normalized position values.
% Convert the cell array of 1x4 position vectors to an nx4 matrix.
% Note: Assumes all objs have 1x4 position vectors (unlike text objs for example)
allPosition = cell2mat(objPos(pass,2));
% Verify positional units (for normalized units only!)
annotationObjs = gobjects(size(allPosition,1),1);
for j = 1:size(allPosition,1)
annotationObjs(j) = annotation(fig, 'rectangle', allPosition(j,:), 'Color','r','LineWidth',10);
Delete the verification rectangles
% Remove annotation outlines




Community Treasure Hunt

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

Start Hunting!

Translated by