Inserting a picture on a GUI

I am doing a GUI for my final project except i can't figure out how to make an image show up. I have tried multiple ways but here looks to be the most successful
I have been creating an axes then creating a callback for that axis. I then edited a few lines to create this:
% --- Executes during object creation, after setting all properties.
function Axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to Axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate Axes1
Axes1(hObject)
imshow('MSD.tif')
I need an image of a Spring-Mass-Damper system.
I tried following the video created by doug hull.

Answers (3)

You would not use Axes1(hObject): you would use axes(hObject)
Or better yet, skip the axes() call and use
imshow(hObject,'MSD.tif')

12 Comments

I am very novice about this. So, I have two questions: 1) If I want the picture (as if it is a logo) in the middle of gui page, where in the mfile of the GUI I should insert the imshow? 2) I assume the picture has to be in the same folder as the mfile?
I wonder if "Axes1" is the name of a routine ?
You can provide the full path in imshow()
I have not tried to figure out the various utility functions created by GUIDE; the generated code is usually so messy that right after I first had a look at it, I took a few months and rewrote our project to not use GUIDE at all. It was worth it.
Reza:
If you're using GUIDE, then put imshow in the OpeningFcn() function. I use GUIDE and don't find it confusing. Sure, the first 50 lines is confusing, but I just ignore that. Every control (button, listbox, etc) has a "click callback function" and you simply put the code you want to execute when you interact with that control into that callback function. I find it easier than having to call uicontrol() for dozens of controls and manually setting up all their sizes, locations, font sizes, colors, ranges, values, etc. To me, that would be a nightmare for all but the simplest GUI.
Second question, the picture does NOT have to be in the same location as the GUI. You can make up the full file name with fullfile() using your actual folder, then, after checking to make sure it actually does exists, read it in from disk and display it, like this:
% Read in a standard MATLAB color demo image.
% Prepare the full file name.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check for existence. Warn user if not found.
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Read it in from disk.
rgbImage = imread(fullFileName);
% Display the original color image.
imshow(rgbImage, []);
title('Original Color Image');
Thank you for yoru resposne. So as I understood your advice I did the following:
function FastDD_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 FastDD (see VARARGIN)
imshow('reza.jpg',[]);
% Choose default command line output for FastDD
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
But, I get the follwing error:
Error using ==> <a href = "matlab: opentoline('C:\Program...\imageDisplayParsInputs.m',126,0')>imageDisplayParseInputs at 126</a>
Invalid input arguments
...
I even tried putting the imshow as: imshow(hObject, 'reza.jpg'); to no avail. Thanks a gain for your help.
What if you imread() reza.jpg and then imshow() that array ?
Reza, look at my code (apparently you totally ignored it), and try it. Change the folder to the folder where your image file lives. If it can't find your image, it will tell you. My guess is that your image is not in your current folder. There is a reason why I write robust code, like checking for existence of files, rather than just letting MATLAB barf up cryptic red text all over my command window.
You can use axes() or pass in the handle, if you have multiple axes controls on your GUI and you need to specify which one, otherwise it will use whatever one it thinks is the current axes.
Now I have:
imread('reza.jpg');
imshow(hObject);
I do not get any error message. The GUI shows up with a blank sapce underneath the several buttons that I have. I am assuming this is an attempt to display the image. But, it is not happening. Or, am I using the imshow wrong? if I take the hObject out i get an error.
MyImage = imread('reza.jpg');
imshow(hObject, MyImage);
Now I am getting this:
??? Error using ==> imageDisplayParseInputs at 176
Invalid input arguments.
Error in ==> imshow at 199
[common_args,specific_args] = ...
%FstDD is my GUI's name
Error in ==> FastDD>FastDD_OpeningFcn at 59
imshow(hObject,MyImage);
Error in ==> gui_mainfcn at 221
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in ==> FastDD at 42
gui_mainfcn(gui_State, varargin{:});
I'm really not sure why I continue to try to help you when you keep refusing my help and ignoring my suggestions. Here's another one to ignore: If you want to do it that way, you'll have to use the 'Parent' property of imshow().
imshow(MyImage, 'Parent', handles.axesImage);
hObject changes depending on what callback you're in so I recommend just using the known and fixed name of the actual axes you will be using (e.g. handles.axesImage), instead of using hObject.
Sorry! I am not trying to ignore anyone. I just have not done this before. I guess my main misunderstanding is from the concept of handles and callback. I have put together a gui in which I like to be able to type in a file name that I can use to output my results into. Then there are two buttons to perform different tasks. Now, I have not quite figured out how to inquire for the output file name and definitely I do not know how to have a logo image on this gui (the problem at hand). I'll follow whatever advice that allows my doing that. What you see is my understanding of your advice, which could be misunderstood version of it. Here is my entire code. Thanks.
function varargout = FastDD(varargin)
% FastDD M-file for FastDD.fig
% FastDD, by itself, creates a new FastDD or raises the existing
% singleton*.
%
% H = FastDD returns the handle to a new FastDD or the handle to
% the existing singleton*.
%
% FastDD('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FastDD.M with the given input arguments.
%
% FastDD('Property','Value',...) creates a new FastDD or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before FastDD_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to FastDD_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 FastDD
% Last Modified by GUIDE v2.5 23-Apr-2012 18:09:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FastDD_OpeningFcn, ...
'gui_OutputFcn', @FastDD_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 FastDD is made visible.
function FastDD_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 FastDD (see VARARGIN)
% Choose default command line output for FastDD
handles.output = hObject;
MyImage=imread('reza.jpg');
imshow(MyImage, 'Parent', handles.axesImage);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes FastDD wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = FastDD_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 RunTask.
function RunTask_Callback(hObject, eventdata, handles)
% hObject handle to RunTask (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 CalcDD.
function CalcDD_Callback(hObject, eventdata, handles)
% hObject handle to CalcDD (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function SubjData_CreateFcn(hObject, eventdata, handles)
% hObject handle to SubjData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
function SubjData_Callback(hObject, eventdata, handles)
% hObject handle to SubjData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%FileID = get(hObject,'String');
Reza, this is getting rather extensive so you should start your own thread rather than piggybacking onto Bryce's thread. I'll solve your problem if you do that.

Sign in to comment.

Also, don't confuse the name of the axes, which is axes1 by default (or handles.axes1 for the full name), with the axes() command. So if you wan't to chose the axes, named axes1, the syntax is:
axes(handles.axes1)
Also, you can use guide (and its property editor), to rename the axes, to avoid confusing yourself.

Asked:

on 10 Mar 2011

Community Treasure Hunt

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

Start Hunting!