In a GUI, I have a function that open a selected image file. I am trying to improve it that if an image is already shown in the axes it will give a question dialog box but it does not work. What is the problem? is there a better and nicer way to do it?
function loadImageButton_Callback(hObject, eventdata, handles)
% hObject handle to loadImageButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.imageAxes, 'Visible','off')
if exists(isappdata(0, 'IData'))
choice = questdlg('An image is already in process, open a new one?', ...
'Load image', ...
'Yes','No','No');
% Handle response
switch choice
case 'Yes'
cla reset;
[file, folder] = uigetfile({'*.jpg;*.gif;*.png;*.tif;*.bmp','All Image Files'},' Select image');
if file == 0;
return;
end
fullFileName = fullfile(folder, file);
I = imread(fullFileName);
setappdata(0,'IData',file);
case 'No'
return;
end
else
[file, folder] = uigetfile({'*.jpg;*.gif;*.png;*.tif;*.bmp','All Image Files'},' Select image');
if file == 0;
return;
end
fullFileName = fullfile(folder, file);
I = imread(fullFileName);
setappdata(0,'IData',file);
end
axes(handles.imageAxes);
imshow(I);
setappdata(handles.imageAxes, 'yourVariable', I);

답변 (1개)

Adam
Adam 2017년 2월 8일

0 개 추천

Declare
handles.hImage = [];
guidata( hObject, handles )
in your OpeningFcn
Then when you plot:
handles.hImage = imshow(...)
guidata( hObject, handles )
and to test if the image exists:
if ~isempty( handles.hImage ) && isgraphics( handles.hImage )
...
end

댓글 수: 3

I added the Declarations in the OpeningFcn:
handles.hImage = [];
guidata( hObject, handles )
And changed the function loadImageButton_Callback to:
function loadImageButton_Callback(hObject, eventdata, handles)
% hObject handle to loadImageButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.imageAxes, 'Visible','off')
if ~isempty( handles.hImage ) && isgraphics( handles.hImage )
choice = questdlg('An image is already in process, would you like top open a new one?', ...
'Load image', ...
'Yes','No','No');
% Handle response
switch choice
case 'Yes'
cla reset;
[file, folder] = uigetfile({'*.jpg;*.gif;*.png;*.tif;*.bmp','All Image Files'},' Select image');
if file == 0;
return;
end
fullFileName = fullfile(folder, file);
I = imread(fullFileName);
setappdata(0,'IData',file);
case 'No'
return;
end
else
[file, folder] = uigetfile({'*.jpg;*.gif;*.png;*.tif;*.bmp','All Image Files'},' Select image');
if file == 0;
return;
end
fullFileName = fullfile(folder, file);
I = imread(fullFileName);
setappdata(0,'IData',file);
end
axes(handles.imageAxes);
handles.hImage =imshow(I);
guidata( hObject, handles )
setappdata(handles.imageAxes, 'yourVariable', I);
But it did not work, what is the error? Why did you advised to add:
handles.hImage = imshow(...)
guidata( hObject, handles )
and to change the if to:
if ~isempty( handles.hImage ) && isgraphics( handles.hImage )
Adam
Adam 2017년 2월 8일
편집: Adam 2017년 2월 8일
"what is the error?"
I don't know, you didn't tell us what aspect of it didn't work.
The code I added saves the image handle when you plot it and then tests if that handle exists and is a valid graphics object.
Lluis Roca
Lluis Roca 2017년 2월 8일
You are right, the error is that when I re-click the button after selecting an image it does not give me the question dialog.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

질문:

2017년 2월 8일

댓글:

2017년 2월 8일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by