display output images from m-files in axes of gui
Show older comments
i have a gui for my m-files , now i have to display my multiple output images in axes of that gui now the thing is even after giving proper handles in m-files ,icant seem to display image in axes after display i want to move from one image to next since i cant my output in my axes im stuck for now

my gui code
function varargout = new5(varargin)
% NEW5 M-file for new5.fig
% NEW5, by itself, creates a new NEW5 or raises the existing
% singleton*.
%
% H = NEW5 returns the handle to a new NEW5 or the handle to
% the existing singleton*.
%
% NEW5('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in NEW5.M with the given input arguments.
%
% NEW5('Property','Value',...) creates a new NEW5 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before new5_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to new5_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help new5
% Last Modified by GUIDE v2.5 17-Apr-2014 21:49:54
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @new5_OpeningFcn, ...
'gui_OutputFcn', @new5_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before new5 is made visible.
function new5_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to new5 (see VARARGIN)
% Choose default command line output for new5
handles.output = hObject;
axes(im,'parent',handles.axes1)
imshow(im)
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes new5 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = new5_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in backbut.
function backbut_Callback(hObject, eventdata, handles)
% hObject handle to backbut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in nextbut.
function nextbut_Callback(hObject, eventdata, handles)
% hObject handle to nextbut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in radiobutton8.
function radiobutton8_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton8
% --- Executes on button press in calpushbutton4.
%function calpushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to calpushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function varargout = calpushbutton4_Callback(h, eventdata, handles,varargin)
hbuttons = [handles.radiobutton8 ];
data = find(get(hbuttons,'Value') == 1);
switch data
case 1
surfacearea
end
% --- Executes on button press in soil.
%function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to soil (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function varargout = soil_Callback(h, eventdata, handles,varargin)
if get(handles.red, 'Value')
% Run function #1
matredhsv;
elseif get(handles.yellow, 'Value')
% Run function #2
matyellowhsv;
else
% Run function #3
matblackhsv;
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
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)
[FileName,FilePath ]= uigetfile('*jpg');
ExPath = fullfile(FilePath, FileName);
set(handles.edit1,'string',ExPath)
% --- Executes on button press in black.
% --- Executes on button press in red.
function red_Callback(hObject, eventdata, handles)
% hObject handle to red (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of red
% --------------------------------------------------------------------
Answers (1)
Image Analyst
on 17 Apr 2014
It doesn't look like a very user friendly GUI to me. You should not ask the user to browse for an image URL, then have different buttons for next and back. What a painful way to select images. I mean, you don't even know what the filenames are or where they're located when you click next or back. A much, much better way is to have a button that specifies the folder where all your images live. Then load up a listbox with the images. Then the user can click on any image in any order to display it. See this GUI framework: http://www.mathworks.com/matlabcentral/fileexchange/24224-magic-matlab-generic-imaging-component for a better way.
And you also didn't name your buttons in a good style. I have no idea from your code whether the next button is pushbutton1 or pushbutton2 or calpushbutton4. You should name them something like btnNext and btnBack, and radRed and radYellow so people know what they control.
But the main problems is you're not calling imshow in any callback to display the image. You call it only in the new5_OpenFcn function, which will probably error out because it has this:
axes(im,'parent',handles.axes1)
imshow(im)
firs of all, im was not defined anywhere in the function so it's not going to know what it is. Secondly you don't use axes like that. The parent option goes into imshow(), not axes(). This is the way it should be done, both there and in any pushbutton callback:
im = ..... however you get it.
axes(handles.axes1);
imshow(im);
5 Comments
nida
on 17 Apr 2014
Joseph Cheng
on 17 Apr 2014
I would suggest if you type in GUIDE into the command window and look at the "GUI with Axes and Menu" template. That implementation should be similar with what you're attempting to do where the single axes changes depending on value/state of another parameter.
Image Analyst
on 17 Apr 2014
Make an axes and make its tag "axesImage". Make a listbox to hold the filenames and call it lstImageList. Make a pushbutton to let the user find the images folder and call it btnSelectFolder. Make a static text label and call it txtFolder.
Here are some code snippets to help you get started:
%=====================================================================
% --- Load up the listbox with image files in folder handles.imageFolder
function handles = LoadImageList(handles)
ListOfImageNames = {};
folder = handles.imageFolder;
if length(folder) > 0
if exist(folder,'dir') == false
msgboxw(['Folder ' folder ' does not exist.']);
return;
end
% fprintf(1, 'Getting list of images in folder: %s\n', folder);
else
fprintf('No folder specified as input for function LoadImageList.\n');
WarnUser('No folder specified as input for function LoadImageList.');
return;
end
% If it gets to here, the folder is good.
ImageFiles = dir([folder '\*.*']);
for Index = 1:length(ImageFiles)
baseFileName = ImageFiles(Index).name;
[folder2, name, extension] = fileparts(baseFileName);
extension = upper(extension);
switch lower(extension)
case {'.png', '.bmp', '.jpg', '.tif'}
% Allow only PNG, TIF, JPG, or BMP images
ListOfImageNames = [ListOfImageNames baseFileName];
otherwise
end
end
set(handles.lstImageList,'string',ListOfImageNames);
% Need to deselect everything otherwise if new folder has fewer files than the last folder used, the listbox won't show up.
set(handles.lstImageList,'Value', []);
return; % from LoadImageList()
%=====================================================================
% --- Executes on clicking btnSelectFolder button.
% Asks user to select a directory and then loads up the listbox (via a call
% to LoadImageList)
function btnSelectFolder_Callback(hObject, eventdata, handles)
% hObject handle to btnSelectFolder (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%msgbox(handles.imageFolder);
returnValue = uigetdir(handles.imageFolder,'Select folder');
% returnValue will be 0 (a double) if they click cancel.
% returnValue will be the path (a string) if they clicked OK.
if returnValue ~= 0
% Assign the value if they didn't click cancel.
handles.imageFolder = returnValue;
handles = LoadImageList(handles);
set(handles.txtFolder, 'string' ,handles.imageFolder);
guidata(hObject, handles);
% Save the image folder in our ini file.
%SaveUserSettings(handles);
end
return; % from btnSelectFolder_Callback()
%=====================================================================
function lstImageList_Callback(hObject, eventdata, handles)
% hObject handle to lstImageList (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns lstImageList contents as cell array
% contents{get(hObject,'Value')} returns selected item from lstImageList
try
% Change mouse pointer (cursor) to an hourglass.
% QUIRK: use 'watch' and you'll actually get an hourglass not a watch.
set(gcf,'Pointer','watch');
drawnow; % Cursor won't change right away unless you do this.
% Get image name
Selected = get(handles.lstImageList, 'Value');
% If more than one is selected, bail out.
if length(Selected) > 1
baseImageFileName = '';
% Change mouse pointer (cursor) to an arrow.
set(gcf,'Pointer','arrow')
drawnow; % Cursor won't change right away unless you do this.
return;
end
ListOfImageNames = get(handles.lstImageList, 'string');
baseImageFileName = cell2mat(ListOfImageNames(Selected));
fullImageFileName = [handles.imageFolder '\' baseImageFileName]; % Prepend folder.
originalImage = imread(fullImageFileName);
imshow(originalImage);
Image Analyst
on 19 Apr 2014
If they're stored to disk, just re-read in the directory and load it into the listbox again. Sometimes I have images that are just "temporary" or "intermediate" and I do not want to save permanently to disk, and for that situation, I use a popup (drop down list) to let the use select which temporary image to display.
Categories
Find more on ROI-Based Processing 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!