User interface Matlab:类似于;相对的;用uicontrol/轴定位;调整大小时保持固定边距
我现在很头痛,想让一个小GUI很好地工作,它不是用GUI编辑器创建的,而是通过编程来创建的!到目前为止,我所了解的情况如下:User interface Matlab:类似于;相对的;用uicontrol/轴定位;调整大小时保持固定边距,user-interface,matlab,resize,figure,uicontrol,User Interface,Matlab,Resize,Figure,Uicontrol,我现在很头痛,想让一个小GUI很好地工作,它不是用GUI编辑器创建的,而是通过编程来创建的!到目前为止,我所了解的情况如下: hFig = figure(); set(hFig, 'Position', [300 200 500 400]); plot((1:10).^2, '*-r'); % Größe des Plots so anpassen, dass links Platz für Buttons ap = get(gca, 'TightInset'); fp = get(gcf,
hFig = figure();
set(hFig, 'Position', [300 200 500 400]);
plot((1:10).^2, '*-r');
% Größe des Plots so anpassen, dass links Platz für Buttons
ap = get(gca, 'TightInset');
fp = get(gcf, 'Position');
set(gca, 'Position', [160/fp(3), 30/fp(4), (fp(3)-180)/fp(3), (fp(4)-60)/fp(4)]);
uicontrol('Style', 'pushbutton', 'String', 'foo', 'Position', [15 fp(4)-60 110 30]);
uicontrol('Style', 'pushbutton', 'String', 'bar', 'Position', [15 fp(4)-100 110 30]);
function myuiFunc(hFig, left, top)
persistent handles;
if(~isempty(handles))
delete(handles);
handles = [];
end
fp = get(hFig, 'Position');
h1 = uicontrol('Style', 'pushbutton', 'String', 'Foo','Position', [left fp(4)-top 100 35]);
h2 = uicontrol('Style', 'pushbutton', 'String', 'Bar','Position', [left fp(4)-top-50 100 35]);
handles = [h1 h2];
end
尝试调整大小:它“看起来”不一样,这意味着uicontrol框不停留在相同的相对位置,并且从轴到图形窗口的边距变大。我想要实现的是:
有一个给定位置(x/y、宽度和高度)的图形窗口,其中有一个绘图。绘图将有一个标题以及x和y的标签。将绘图设置为高度和宽度,使紧插图加上每个方向上的边距,其大小与图形窗口一样大(例如,紧插图+10px);除了在左侧留出150px的可用空间来放置一些uicontrol按钮,并使它们保持在相同的位置:这与能够从上/左(上=20,左=10)而不是从下/左给出位置相同
非常感谢 好的,终于找到了我想要的有效解决方案:-)希望对感兴趣的人有帮助: 主脚本文件:
p = [300 300 1000 600];
fixedMargins = [250 0 0 0]; % [left, top, right, bottom]
f = figure('Position', p, 'Color', [0.9 0.9 0.9]);
plot(-10:10, (-10:10).^3, '*-r');
set(f, 'ResizeFcn', {@resizeCallback, gca, fixedMargins, {@myuiFunc, f, 40, 50}});
title('bla')
xlabel('foooooooooo')
ylabel('barrrrrrr')
调整回调函数的大小:
% Need to pass the handle of the axis to modify (hAx) AND to pass the
% desired margins as second extra callback argument:
% [left, top, right, bottom]!
function resizeCallback(hFig, ~, hAx, fixedMargins, func)
% Disable automatic rezising
set(hAx, 'Units', 'pixels');
% Figure-Size
fp = get(hFig, 'Position');
% Calculate Position of the axis
margin = get(hAx, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1];
% Position to fill the figure minus the TightInset-Margin
newPos = [0 0 fp(3:4)] - margin;
% Change position based on margins
newPos(1) = newPos(1) + fixedMargins(1);
newPos(3) = newPos(3) - fixedMargins(1) - fixedMargins(3);
newPos(2) = newPos(2) + fixedMargins(4);
newPos(4) = newPos(4) - fixedMargins(2) - fixedMargins(4);
% Set new position
set(hAx, 'Position', newPos);
% Call UI-Func
if(nargin == 5)
f = func{1};
args = func(2:end);
f(args{:});
end
end
在调整地物窗口的大小时,您可以传递希望调用的任何函数,例如更新地物中的某些内容。在我的示例中,它是myuiFunc(),如下所示:
hFig = figure();
set(hFig, 'Position', [300 200 500 400]);
plot((1:10).^2, '*-r');
% Größe des Plots so anpassen, dass links Platz für Buttons
ap = get(gca, 'TightInset');
fp = get(gcf, 'Position');
set(gca, 'Position', [160/fp(3), 30/fp(4), (fp(3)-180)/fp(3), (fp(4)-60)/fp(4)]);
uicontrol('Style', 'pushbutton', 'String', 'foo', 'Position', [15 fp(4)-60 110 30]);
uicontrol('Style', 'pushbutton', 'String', 'bar', 'Position', [15 fp(4)-100 110 30]);
function myuiFunc(hFig, left, top)
persistent handles;
if(~isempty(handles))
delete(handles);
handles = [];
end
fp = get(hFig, 'Position');
h1 = uicontrol('Style', 'pushbutton', 'String', 'Foo','Position', [left fp(4)-top 100 35]);
h2 = uicontrol('Style', 'pushbutton', 'String', 'Bar','Position', [left fp(4)-top-50 100 35]);
handles = [h1 h2];
end
我喜欢:)希望你也喜欢
编辑:无需编辑resizeCallback函数!如果您只是将所需的页边距传递给它,并且如果您愿意,还可以添加一个带有参数的函数句柄,该函数句柄将在每次调整大小时调用 您也可以使用“标准化”单位。
uicontrol('Style'、'button'、'String'、'foo'、'units'、'Normalized'、'Position'、[0.90 0.05 0.08 0.08])代码>真的没有人知道答案吗?这对我来说非常重要,我就是不能让它正常工作!很抱歉,为什么要否决这一点呢?我没有收到一些人的来信,因为这是一个合理的问题,我自己也贴了一个很好的答案。很好的一个,我已经找了很多年了:-)请告诉我怎么做?实际上,我不想让按钮调整大小!在这种情况下,你不能使用。我从帖子上没意识到你不想让按钮动。