Select file issue in Matlab GUI
1 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
回答(1 个)
Adam
2017-2-8
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!