Problem displaying an image on an axis

1 view (last 30 days)
Samah EL QASSAH
Samah EL QASSAH on 1 Dec 2016
Commented: Adam on 1 Dec 2016
Hi!
I have a GUI that allows to display an image on an axis by clicking on a start button. The code is below:
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
start(handles.t);
set(handles.showColor, 'backgroundColor', 'green');
% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
stop(handles.t);
% --- Executes on button press in showColor.
function showColor_Callback(hObject, eventdata, handles)
% hObject handle to showColor (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function Disp_image(obj, event, handles)
axes(handles.axes1);
img = imread('image\electronique.png');
image(img);
axis(handles.axes1, 'image', 'off');
The image appears on the axis that I have defined but it also displayed on another figure:
Why the image appears on the 2nd figure? I can't understand.
PS: I put the function "Disp_image" because I will need it to add other commands.

Answers (2)

Adam
Adam on 1 Dec 2016
Edited: Adam on 1 Dec 2016
Always give an explicit axes handle to plotting instructions. Almost all plotting instructions (certainly in base Matlab) allow you to pass an axes handle as the first argument and most of those that don't allow you to specify {'Property', 'Value'} pairs where you can include
'Parent', hAxes
In this case use
image( handles.axes1, img );
  2 Comments
Samah EL QASSAH
Samah EL QASSAH on 1 Dec 2016
I add the 'handles.axes1' as shown here:
function Disp_image(obj, event, handles)
axes(handles.axes1);
img = imread('image\electronique.png');
image(handles.axes1, img);
axis(handles.axes1, 'image', 'off');
But the image is not displayed
%
Adam
Adam on 1 Dec 2016
Are you using an old version of Matlab? I don't know in which version support for the axes handle as first argument was added for image, but for imagesc it was very recent, so you may instead need
image( img, 'Parent', handles.axes1 )

Sign in to comment.


Samah EL QASSAH
Samah EL QASSAH on 1 Dec 2016
If I change the axis by a button, the result is as follows:
function Disp_image(obj, event, handles)
img = imread('image\electronique.png');
set(handles.affich_image, 'CData', img);
Does the function fail to know the axis?

Categories

Find more on Graphics Object Properties 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!