How to automatically resize custom button.CData image with maximize and restore down clicks on guide?

4 views (last 30 days)
Hi,
I am trying to use cData feature to change the look of each button to some image.
I was able to acheive this but when I try to use maximize and restore down clicks on guide, the image size is not being updated to fit the normalized size of the button.
Can we make the button.CData resize the image when we do change the size of GUI using maximize or downclick or drag features on the guide?
My code:
I put the image fit to button size in OpeningFcn like this:
% --- Executes just before tempGUI is made visible.
function tempGUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles = guidata(hObject);
[button_img,~]=imread('peppers.png');
handles.openPeppers.Units = 'pixels';
button_img =imresize(button_img,fliplr(handles.openPeppers.Position(1,3:4)));
handles.openPeppers.CData = button_img;
handles.openPeppers.Units ='normalized';
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
Then, if I change the size of guide manually , I need to dump the same code into button again to fit into button size.
% --- Executes on button press in openPeppers.
function openPeppers_Callback(hObject, eventdata, handles)
% hObject handle to openPeppers (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
button_img = handles.openPeppers.CData;
handles.openPeppers.Units = 'pixels';
button_img =imresize(button_img,fliplr(handles.openPeppers.Position(1,3:4)));
handles.openPeppers.CData = button_img;
handles.openPeppers.Units ='normalized';
% Update handles structure
guidata(hObject, handles);
Sample Images:
After OpeningFcn looks fine:
Capture.JPG
When I resize the guide manually, it doesn't fit and need to press button:
Capture.JPG
Can I do this without button click option?
Thanks,
Gopi

Accepted Answer

Geoff Hayes
Geoff Hayes on 13 Aug 2019
Gopichandh - try using the ResizeFcn callback. You can add this callback from the GUIDE editor by right-clicking on the figure and selecting View Callbacks --> ResizeFcn. This should create a function similar to
% --- Executes when figure1 is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
in your m file. You can then add the code to update the button image. I suggest that you create a helper function (that updates the button) which can be called from both your OpeningFcn and ResizeFcn so that you avoid code duplication.
  1 Comment
Gopichandh Danala
Gopichandh Danala on 14 Aug 2019
Edited: Gopichandh Danala on 14 Aug 2019
Wow, I found SizeChangedFcn in View Callbacks. I dumped my code in both OpeningFcn and SizeChangedFcn.
It works perfect as expected.
Thanks Geoff!

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!