How can I change uipanel dimensions by using user input?

134 Views Asked by At

I'm trying to change panel and axes width and height values by using user input. These values will represent a photograph's resolution. For example, if a user inputs 512*512, the uipanel and Axes' width and height will change to 512 and the user will work on this workspace.

What I tried so far:

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});

but the size of uipanel1 does not change.

2

There are 2 best solutions below

4
On BEST ANSWER

Here's code that demonstrates how what you want can be done, which works with (default from MATLAB 2014b and onward).

%% //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;

On older versions of MATLAB, you may need slightly different steps:

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
0
On

You should use setcommand to modify the UI component properties (and get command to retrieve information). In your case:

% 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})]);