Programmatically adjust the width of Document Bar
2 views (last 30 days)
Show older comments
I am looking for a way to programatically adjust the width of this panel, does anyone have any ideas?
I am using setDocumentBarPosition to move this panel to the left side onf the window. I would like to adjust it's width but can't figure out how. I am able to get it's current inset value using getGroupContainerInsets but am stuck at trying to figure out how to modify it. there is definitely something that matlab is storing in the background because it remembers the width when i re-create the group in a different instance of matlab.
0 Comments
Answers (1)
Karan Singh
on 19 Jun 2024
Hi Abhishek,
To programmatically adjust the width of a panel in MATLAB, you can interact with the underlying Java components that MATLAB uses for its GUI. The following approach allows you to manipulate the width of a panel.
First, you need to get a handle to the Java component that represents your panel. This can be done using the “findjobj” function from the MATLAB File Exchange. Here is the link to download the same:- https://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects
After installation, Now we can proceed with the following example on how you can achieve this:
% Create Figure 1
hFig1 = figure('Position', [100, 100, 800, 600]);
% Create Panel 1 in Figure 1 and set its initial position and size
hPanel1 = uipanel('Parent', hFig1, 'Title', 'Panel 1', 'Position', [0, 0, 0.3, 1]);
% Function to adjust the panel width using MATLAB properties
function adjustPanelWidth(hPanel, newWidth)
% Get the current position
pos = get(hPanel, 'Position');
% Set the new width
pos(3) = newWidth;
% Update the panel position
set(hPanel, 'Position', pos);
% Print the new position for debugging
disp('New Position:');
disp(pos);
end
% Adjust the panel width for Panel 1 in Figure 1 to 0.3 (normalized units)
adjustPanelWidth(hPanel1, 0.3);
% Create Figure 2
hFig2 = figure('Position', [100, 100, 800, 600]);
% Create Panel 2 in Figure 2 and set its initial position and size
hPanel2 = uipanel('Parent', hFig2, 'Title', 'Panel 2', 'Position', [0, 0, 0.6, 1]);
% Adjust the panel width for Panel 2 in Figure 2 to 0.6 (normalized units)
adjustPanelWidth(hPanel2, 0.6);
% Optional: Adjust the positions to make sure they are not overlapping
pos1 = get(hPanel1, 'Position');
pos2 = get(hPanel2, 'Position');
pos2(1) = 0; % Set the starting x position of Panel 2 to the leftmost edge (0)
set(hPanel2, 'Position', pos2);
% Print the final positions for debugging
disp('Final Position Panel 1:');
disp(get(hPanel1, 'Position'));
disp('Final Position Panel 2:');
disp(get(hPanel2, 'Position'));
The “adjustPanelWidth” function takes the panel handle and a new width as input. You can try it out in your use case.
Hope it helps!
0 Comments
See Also
Categories
Find more on Software Development Tools in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!