MATLAB: Hello i am working on GUI and i wrote a simple code that get the position of the pushbutton once it is pressed then send it thruogh the UserData property to the uipushtool which will display a resized picture on the pushbutton CData

gui userdata cdata

function pushbutton3_Callback(hObject, eventdata, handles)
p=getpixelposition(hObject);
set(handles.uipushtool1,'UserData',p); % get the pushbutton position and put it in the User data of the uipushtool1
% --------------------------------------------------------------------
function uipushtool1_ClickedCallback(hObject, eventdata, handles)
[x,map]=imread('NC.PNG');
I2=imresize(x, [40 100]);
p=get(hObject,'UserData') % get the position of the pushbutton back from User data of the uipushtool1
h=findobj('position',p) % Find object which has the positon stored in the userdata (P)
set(h,'CData',I2) % put my picture in the object h specified by its position

Best Answer

Are the pushbutton's units pixels? If not then it won't work. If so then it is still an unsafe way to do things. You are relying on precise equality measures of double-precision floating point numbers which may or may not work depending on rounding and all the other usual issues with exact comparisons of floats.
Why don't you just use the tag property of the pushbutton to identify it instead of relying on its position? That is what the tag is there for, especially if you name it something sensible rather than keeping the default of pushbutton3. Since you are using findobj with a property, value pair you can use any property of the pushbutton that is most suitable and this would usually be the tag.
In fact, you can cut out using a property at all and just store the pushbutton handle itself:
set(handles.uipushtool1,'UserData',hObject)
then
h =get(hObject,'UserData');
set(h,'CData',I2)