How to show an image once starting GUI

3 views (last 30 days)
Hi all
I set code below on axes_background_CreateFcn, it works fine, but sometimes I re-run the GUI, axes tag will become blank and unable to show my image.
function axes_background_CreateFcn(hObject, eventdata, handles)
axes(hObject);
imshow('main_bg.jpg');

Accepted Answer

Image Analyst
Image Analyst on 19 Nov 2011
I think it's best if you put the imshow() in the OpeningFcn() rather than the create function. The OpeningFcn is the function where you're supposed to put all your startup code - see the comments for it.
Other problems with your code are that
1. You failed to use the full file name by using fullfile() to prepend the folder. See the FAQ ( http://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function. )
2. You failed to check for existence of the file with the exist(fullFileName, 'file') function.
3. You failed to use try/catch,
try
fullFuleName = fullfile(folder, 'main_bg.jpg');
if exist(fullFileName, 'file)
imshow(fullFilename);
else
errorMessage = sprintf('Error: file does not exist:\n%s', fullFileName);
uiwait(warndlg(errorMessage));
end
catch ME
% Some general error.
errorMessage = sprintf('Error in function axes_background_CreateFcn.\n\nError Message:\n%s', ME.message);
uiwait(warndlg(errorMessage));
end
You can improve the robustness of your code if you read and follow the guidelines here: http://www.mathworks.com/matlabcentral/answers/1148-how-to-learn-matlab

More Answers (1)

ws
ws on 20 Nov 2011
Hi guys
I'm not sure the method used is correct, but I have my image to appear on my main uipanel:
function axes_background_CreateFcn(hObject, eventdata, handles)
axes(handles.axes21)
imshow('main_bg.jpg')
:)

Categories

Find more on Migrate GUIDE Apps 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!