How to Open file with GUI's axes multiple times?

Hi,
With the code below i'm able to read a file via GUI when a button is pressed. The code pop out a windows that enable user to search for file. However now, instead of using a push botton, i want to use axes. when user click on an axes, a pop out window will come out asking user to search for file (similar to File>> Open File), then display the image on my axes. How do i write the code for my axes function?
function btnGetFile_Callback(hObject, eventdata, handles)
% hObject handle to btnGetFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global fullpath;
[filename,pathname]= uigetfile(...
{'*.bmp;*.jpg;*.jpeg;*.tif;*.tiff;*.png','All Image Files(*.bmp,*.jpg,*.jpeg,*.tif,*.tiff,*.png)';...
'*bmp','bitmap Files (*.bmp)';...
'*.jpg;*.jpeg','JPEG Files(*.jpg,*.jpeg)';...
'*.tif;*.tiff','Tiff Files(*.tif,*.tiff)';...
'*png','PNG Files (*.png)';...
'*.*','All Files (*.*)'}, ...
'Pick an image file');
fullpath = sprintf('%s%s',pathname, filename);
set(handles.editFile, 'string',fullpath);
Thanks

 Accepted Answer

% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName] = uigetfile({'*.jpg';'*.bmp';'*.gif';'*.tif'},'Select the image')
cb=get(gca,'ButtonDownFcn');
if (~strcmp(class(FileName),'double'))
A = imread(fullfile(PathName,FileName));
imagesc(A)
end
%restore the ButtonDownFcn of the axes that imagesc removed
set(gca,'ButtonDownFcn',cb)
set(get(gca,'Children'),'ButtonDownFcn',cb)

5 Comments

btw how do i make it to be able to load n change image again. I notice after the image is on the axes. The axes no longer able to read n display new image.
Yes you are right, the stupid imagesc function clears the settings for the current axes including the ButtonDownFcn, I will look into that problem when I have the time, meanwhile someone else might have a way to solve it.
I did try to restore the current axes ButtonDownFcn
[FileName,PathName] = uigetfile({'*.jpg';'*.bmp';'*.gif';'*.tif'},'Select the image')
cb=get(gca,'ButtonDownFcn')
if (~strcmp(class(FileName),'double'))
A = imread(fullfile(PathName,FileName));
imagesc(A)
end
set(gca,'ButtonDownFcn',cb)
But the image seems to be on top of the axes, only clicking on the ticks works properly
It's fixed on my answer, what happens is that imagesc removes the ButtonDownFcn from the axes and the image that appears on the axes is children of that axes so it has it's own ButtonDownFcn, my code solves it.

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings 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!