如何使用用户输入更改 uipanel 尺寸?

How can I change uipanel dimensions by using user input?

我正在尝试使用用户输入更改面板和轴的宽度和高度值。这些值将代表照片的分辨率。例如,如果用户输入 512*512,则 uipanelAxes 的宽度和高度将更改为 512,并且用户将在此工作区上工作。

到目前为止我尝试了什么:

prompt = {'Enter width', 'Enter height'}; 
dlg_title = 'Input'; num_lines = 1; def = {'256','256'};
answer = inputdlg(prompt,dlg_title,num_lines,def); 
uipanel1.width = str2num(answer{1}); 
uipanel1.height = str2num(answer{2});

但是 uipanel1 的大小没有改变。

这里的代码演示了如何完成您想要的工作,它适用于 (MATLAB 2014b 及更高版本的默认设置)。

%% //Create a figure with a uipanel & axes
hFig = figure('Units','pixels');       %// Create a new figure
hFig.Position = [100 100 600 600];     %// Adjust figure's position
hPan = uipanel(hFig,'Units','pixels'); %// Create a new panel
hPan.Position = [150 150 300 300];     %// Adjust panel's position
hAx = axes('Parent',hPan,'Units','normalized','Position',[0 0 1 1]); %// New axes
%% //Ask for user input
prompt = {'Enter width', 'Enter height'}; 
dlg_title = 'Input'; num_lines = 1; def = {'256','256'};
answer = cellfun(@str2double,inputdlg(prompt,dlg_title,num_lines,def)); 
%% //Modify the panel's position (axes will stretch\shrink automatically to fit)
hPan.Position(3:4) = answer;

在旧版本的 MATLAB 上,您可能需要稍微不同的步骤:

new_pos_vec = get(hPan,'Position'); %// Get the existing values
new_pos_vec(3:4) = answer;          %// Modify just the width & height
set(hPan,'Position', new_pos_vec);  %// Update graphical properties

您应该使用 set 命令来修改 UI 组件属性(以及 get 命令来检索信息)。你的情况:

% get current position
currentPosition = get(uipanel1, 'position');

% update the position with the entered values
set(uipanel1, 'position', [currentPosition(1), currentPosition(2), str2num(answer{1}), str2num(answer{2})]);