convert normal GUI cordinates to normalized cordinates

10 次查看(过去 30 天)
How to convert normal GUI cordinates to normalized cordinates.
How to write a function for that?
  6 个评论
Praneeti Mitra
Praneeti Mitra 2021-5-9
It is not created in app designer Units should normalized and right now its in points
Adam Danz
Adam Danz 2021-5-11
Then the solution in my answer should work. Have you tried it?

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2021-5-9
编辑:Adam Danz 2021-5-9
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
fig = _____________; % FILL THIS IN WITH YOUR GUI'S 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)
try
figChil(i).Units = outUnits;
objPos{i,2} = figChil(i).Position; % POSITION DATA
catch
pass(i) = false; % POSITION DATA
end
end
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);
end
Delete the verification rectangles
% Remove annotation outlines
delete(annotationObjs)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by