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

时间:2015-06-14 06:13:20

标签: matlab matlab-figure

我正在尝试使用用户输入更改面板和轴的宽度和高度值。这些值将代表照片的分辨率。例如,如果用户输入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的大小不会改变。

2 个答案:

答案 0 :(得分:0)

这里的代码演示了如何完成您的工作,它适用于(默认来自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

答案 1 :(得分:0)

您应该使用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})]);