UItabgroup does not save position within UIfigure using saveas or savefig
显示 更早的评论
I am having issues using saveas with UItabgroups. It seems the position of the uitabgroup is not saved when using saveas, and defaults to a smaller portion of the figure when opened again. I have seen a few similar questions with no concrete answers, a few users had suggested it's a "bug" but all other UIfigure properties I add save fine.
%initialize figure
hfig1 = uifigure;
hfig1.WindowState = 'maximized';
htabgroup = uitabgroup(hfig1,"SelectionChangedFcn",@displaySelection);
htabgroup.Position = [0 0 1600 800];
%% Button for save
save_btn = uibutton(hfig1,"Text","Save Figure","Position",[1610 740 75 25]);
save_btn.ButtonPushedFcn = {@saveFig,hfig1};
function saveFig(src,event,hfig)
%Get new file name
prompt2 = {'Would you like to save the figure? If yes, please enter desired name below, otherwise hit okay'};
dlgtitle2 = 'Input';
fieldsize2 = [1 150];
definput2 = {'Figure_file_name'};
fig_file_cell = inputdlg(prompt2,dlgtitle2,fieldsize2,definput2);
%Covnert file name and save
fig_file_save = convertCharsToStrings(char(fig_file_cell));
saveas(hfig,fig_file_save,'fig')
end
6 个评论
Walter Roberson
2024-11-1
Madheswaran
2024-11-5
Hi @Bennett
I tried to reproduce the issue with a similar setup but wasn't able to replicate the behavior you're describing. In my testing, both the position and window state were preserved when using 'saveas' function to save the figure and reopening it using 'openfig'.
For debugging I suggest you try the following:
- Check if the issue occurs with a simpler test case (without tabs)
- Check if you are doing any other window manipulations
- Print the figure position and window state before saving and after opening to check what changed in detail
As a workaround, you could also try storing the position and window state explicitly using 'UserData' property of 'UIFigure' in the 'saveFig' function:
fig.UserData.SavedPosition = fig.Position;
fig.UserData.SavedState = fig.WindowState;
While opening the figure, update the 'Position' and 'WindowState' of the figure as per the 'UserData'.
fig = openfig(filename);
if isfield(fig.UserData, 'SavedState')
fig.WindowState = fig.UserData.SavedState;
end
if isfield(fig.UserData, 'SavedPosition')
fig.Position = fig.UserData.SavedPosition;
end
Let me know if this resolves the issue you are facing!
Bennett
2024-11-6
Bennett
2024-11-6
Walter Roberson
2024-11-6
You did not answer the question of why you are using saveas() instead of savefig() ?
Bennett
2024-11-7
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

