How to include a picture in .fig using GUIDE and no .m file

The task is to include a picture in a .fig file using GUIDE, without using and generating the .m file (Tool --> GUIoption --> Generate FIG file only). It's clear I need to insert a Axes Box and using createFcn try to insert the picture. It seems that GUIDE does not recognise Matlab function like imshow('name.png').
Any ideas how to do it?

 Accepted Answer

GUIDE recognizes imshow(). You just need to have an axes on the figure before you use it (I think, though it may create a new figure with an axes on it if you don't already have an axes). imshow() used to be part of the Image Processing Toolbox but in 2015 it moved into base MATLAB.

4 Comments

ok, perfect. I first create an axes component. When it's saved, in createFcn I try imshow(). GUIDE doesn't let me edit createFcn if I enter imshow('a.png'), as guide cannot access to the folder where .fig is located and charge the picture. neither imread('a.png') works. If I put imshow() [I know it's stupid without any inputs] GUIDE let me perfectly edit the box 'createFcn'....
Don't put anything into any CreateFcn(). I never do. That's not the place for it. Initialize any properties in GUIDE, not the CreateFcn. If you want to do some more initialization at launch, then do it in the OpeningFcn for the whole GUI, not in individual CreateFcn functions of the individual controls.
To put an image in, if the image is not in the same folder as the .fig file, and not on the search path, then use fullfile() and exist:
folder = 'c:/whatever'; % or pwd or wherever it is.
baseFileName = 'a.png';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
message = sprintf('File not found"\n%s', fullFileName);
uiwait(warndlg(message));
else
theImage = imread(fullFileName);
imshow(theImage);
end
Remember, put the above code in the OpeningFcn() function, not any CreateFcn() function.
Why there is no OpeningFcn available for the individual controls? And where could I edit/defining an OpeningFcn for the whole GUI?
Remember I'm just generating the .fig file; in this case I don't want to write in any .m files
GUIDE always creates an m-file. There has to be code to create a GUI. If you don't want that, use Photoshop.
The OpeningFcn is for the whole GUI. Individual controls have something like that with their CreateFcn, though in my observations, you just get into trouble if you start messing with those. Besides, you don't need to -- anything you would do in the CreateFcn can be done directly in GUIDE's property inspector, or in the OpeningFcn. Don't worry about it - it's just the way it works.

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!