In matlab GUI, how to show pictures in an axes that has already been used to show pictures?

2 views (last 30 days)
What I want to do is click a button and input a keyword so that the pictures corresponding to this keyword will be showed in an axes of GUI. However, I can only search the pictures once without restart the program, if I click the search button and search another keyword, there will be an error says the axes handle is invalid. So would somebody please tell me how can I solve this problem? Part of the code as flows:
if true
% code
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
prompt={'What kind of emoticons you want to find?'};%Prompt.
name='Search Emoticon';%Headline
numlines=1;%Number of rows of input data.
defAns={''};%Default value.
Resize='on';%Set the size of dialog can be adjusted.
answer=inputdlg(prompt,name,numlines,defAns,'on');%Building an input dialog.
A=answer;
global Emoticon
Emoticon=upper(A);
if strcmp('HAPPY',Emoticon) %Compare the value of Emoticon with 'HAPPY'.
figure('visible','off')
axes(handles.axes1) %Show pictures in axes1.
subplot(2,2,1);imshow('happy1.jpg');
subplot(2,2,2);imshow('happy2.jpg');
subplot(2,2,3);imshow('happy3.jpg');
subplot(2,2,4);imshow('happy4.png');
elseif strcmp('ANGRY',Emoticon)
figure('visible','off')
axes(handles.axes1)
subplot(2,2,1);imshow('angry1.jpg')
subplot(2,2,2);imshow('angry2.jpg')
subplot(2,2,3);imshow('angry3.jpg')
subplot(2,2,4);imshow('angry4.jpg')

Answers (1)

Walter Roberson
Walter Roberson on 30 Sep 2017
Each of those
figure('visible','off')
calls creates a new figure, but sets it invisible. You then do
axes(handles.axes1) %Show pictures in axes1.
which changes the focus to the existing axes1 on an existing figure, which changes the current figure over to the one holding axes1 . The figure created by figure('visible','off') is left hanging around unused.
After that you
subplot(2,2,1);imshow('happy1.jpg');
subplot() calculates the axes position implied by its arguments (in this case, the upper left quarter). Then it looks through all the existing axes in the figure, and if it finds any existing axes that overlap that just-calculated position, but which is not exactly the same position, then it deletes that existing axes. If it found an axes with exactly the calculated position then it uses that existing axes, and otherwise it creates a new axes. The same thing happens for your other subplot() calls.
Your four subplot calls together are going to cover most of the figure that handles.axes1 was on. Chances are high that one or more of them overlapped the place you had put handles.axes1, so chances are high that the subplot() calls have deleted handles.axes1 .
Then, with handles.axes1 having been deleted, when you make the
axes(handles.axes1)
call the next time, the operation cannot be completed.
Note: you do not draw anything on handles.axes1, so provided you stop creating those extra figures that you do not do anything with, you can just leave the line out and count on the current figure not having changed. Though you would have the same problem if you refer to handles.axes1 somewhere else in the code.

Categories

Find more on 指定图形输出的目标 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!